Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Concrete/WoocommercePlatformOrderDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ private function extractBasePaymentData()
$newPaymentData->identifier = $identifier;
$newPaymentData->installments = intval($this->formData["installments"]);
$newPaymentData->recurrenceCycle = $this->formData["recurrence_cycle"] ?? null;
$newPaymentData->recurrenceModel = $this->formData["recurrence_model"] ?? null;
$newPaymentData->paymentOrigin = $this->formData["payment_origin"] ?? null;
$newPaymentData->saveOnSuccess = isset($this->formData["save_credit_card"]);
$amount = $this->formData["card_order_value"] ?? $this->getGrandTotal();
Expand Down
1 change: 1 addition & 0 deletions src/Model/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes:
if ($type === CheckoutTypes::TRANSPARENT_VALUE) {
$fields = $this->convertCheckoutObject($_POST[PaymentRequestInterface::PAGARME_PAYMENT_REQUEST_KEY]);
$fields['recurrence_cycle'] = Subscription::getRecurrenceCycle();
$fields['recurrence_model'] = Subscription::getRecurrenceModel();
$this->formatFieldsWhenIsSubscription($fields, $wc_order);
$attempts = intval($wc_order->get_meta('_pagarme_attempts') ?? 0) + 1;
$wc_order->update_meta_data("_pagarme_attempts", $attempts);
Expand Down
85 changes: 85 additions & 0 deletions src/Model/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ private function convertOrderObject(Order $order)
$fields['card_id'] = $card['cardId'];
$fields['pagarmetoken'] = $card['cardId'];
$fields['recurrence_cycle'] = "subsequent";
$fields['recurrence_model'] = self::getRecurrenceModelFromOrder($order->getWcOrder());
$fields['payment_origin'] = isset($card['chargeId']) ? ["charge_id" => $card['chargeId']] : null;
}

Expand Down Expand Up @@ -374,6 +375,90 @@ protected function getCardDataByTransaction($transactions)
return $transactions->getCardData();
}

/**
* Determines whether the subscription has a fixed end date (finite billing cycles)
* @param int $productId
* @return bool
*/
private static function hasFixedEndDate(int $productId): bool
{
$length = (int) WC_Subscriptions_Product::get_length($productId);
return $length > 0;
}

/**
* Determines the recurrence model for a subscription product
* @param int $productId
* @return string 'subscription' for fixed-duration or 'standing_order' for indefinite
*/
private static function getRecurrenceModelFromProductId(int $productId): string
{
if (self::hasFixedEndDate($productId)) {
return 'subscription';
}
return 'standing_order';
}

/**
* Extracts the recurrence model from the first subscription product in the cart
* @return string|null 'subscription', 'standing_order', or null if no subscription
*/
public static function getRecurrenceModel(): ?string
{
if (!self::canProcessSubscriptions()) {
return null;
}
return self::extractRecurrenceModelFromCartItems();
}

/**
* Determines whether subscriptions can be processed in the current context
* @return bool
*/
private static function canProcessSubscriptions(): bool
{
return self::hasSubscriptionPlugin() && self::hasSubscriptionProductInCart();
}

/**
* Extracts recurrence model from the first subscription item in cart
* @return string|null
*/
private static function extractRecurrenceModelFromCartItems(): ?string
{
foreach (WC()->cart->cart_contents ?? [] as $item) {
return self::getRecurrenceModelFromProductId((int) $item['product_id']);
}
return null;
}

/**
* Extracts the recurrence model from the first subscription product in an order
* Used when processing subscription renewals
* @param WC_Order $wcOrder
* @return string|null 'subscription', 'standing_order', or null if no subscription
*/
public static function getRecurrenceModelFromOrder(WC_Order $wcOrder): ?string
{
if (!self::hasSubscriptionPlugin()) {
return null;
}
return self::extractRecurrenceModelFromOrderItems($wcOrder);
}

/**
* Extracts recurrence model from the first subscription item in order
* @param WC_Order $wcOrder
* @return string|null
*/
private static function extractRecurrenceModelFromOrderItems(WC_Order $wcOrder): ?string
{
foreach ($wcOrder->get_items() as $item) {
return self::getRecurrenceModelFromProductId((int) $item->get_product_id());
}
return null;
}

/**
* @return string|null
*/
Expand Down
Loading