Skip to content

Commit

Permalink
fix: get recurrence payment in subscription renew
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaMelazzo committed Oct 7, 2024
1 parent 2f2989b commit 2ec70c8
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions src/Model/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Subscription
/** @var AbstractGateway */
private $payment;

private $logger;

/** @var array */
const ONE_INSTALLMENT_PERIODS = ['day', 'week'];

Expand All @@ -52,6 +54,7 @@ public function __construct(
$this->orders = new Orders;
$this->addSupportToSubscription();
$this->setPaymentEnabled();
$this->logger = new LogService('Renew Subscription');
}

private function addSupportToSubscription(): void
Expand Down Expand Up @@ -338,21 +341,66 @@ private function convertOrderObject(Order $order)
* @param $paymentMethod
* @return array
*/
private function formatPaymentMethod($paymentMethod)
private function formatPaymentMethod($paymentMethod): array
{
$paymentMethod = str_replace('woo-pagarme-payments-', '', $paymentMethod);
return str_replace('-', '_', $paymentMethod);
}

/**
* @param $order
*
* @return false|mixed
*/
private function getCardSubscriptionData($order)
{
$cardData = $order->get_meta("pagarme_payment_subscription");
if (!$cardData) {
$cardData = $order->get_meta('pagarme_payment_subscription');

if (empty($cardData)) {
$this->logger->info('Card data not found in the order meta.');
$cardData = $this->getCardSubscriptionDataFromSubscriptionOrParentOrder($order->ID);
}

if (empty($cardData)) {
return false;
}

return json_decode($cardData, true);
}

/**
* @param $orderId
*
* @return array|false|mixed|string
*/
private function getCardSubscriptionDataFromSubscriptionOrParentOrder($orderId)
{
$subscriptions = wcs_get_subscriptions_for_order($orderId, ['order_type' => 'any']);
foreach ($subscriptions as $subscription) {
if ($subscription->get_type() !== 'shop_subscription') {
continue;
}

$cardData = $subscription->get_meta('_pagarme_payment_subscription');

if (!empty($cardData)) {
return $cardData;
}

$this->logger->info('Card data not found in the subscription meta.');
$parentOrder = wc_get_order($subscription->get_data()['parent_id']);
$cardData = $parentOrder->get_meta('pagarme_payment_subscription');

if (!empty($cardData)) {
return $cardData;
}
}

$this->logger->info('Card data not found in the subscription parent order meta.');

return false;
}


private function getCardDataByResponse($response)
{
Expand Down

0 comments on commit 2ec70c8

Please sign in to comment.