Skip to content

Commit

Permalink
Fix update payment source on WC Order subscription renewals.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosilva-pt committed Feb 5, 2024
1 parent 977f983 commit 6a99a9b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion modules/ppcp-wc-subscriptions/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$environment = $container->get( 'onboarding.environment' );
$settings = $container->get( 'wcgateway.settings' );
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$funding_source_renderer = $container->get( 'wcgateway.funding-source.renderer' );
return new RenewalHandler(
$logger,
$repository,
Expand All @@ -36,7 +37,8 @@
$payer_factory,
$environment,
$settings,
$authorized_payments_processor
$authorized_payments_processor,
$funding_source_renderer
);
},
'wc-subscriptions.repository.payment-token' => static function ( ContainerInterface $container ): PaymentTokenRepository {
Expand Down
43 changes: 42 additions & 1 deletion modules/ppcp-wc-subscriptions/src/RenewalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenVenmo;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
Expand Down Expand Up @@ -108,6 +109,13 @@ class RenewalHandler {
*/
protected $authorized_payments_processor;

/**
* The funding source renderer.
*
* @var FundingSourceRenderer
*/
protected $funding_source_renderer;

/**
* RenewalHandler constructor.
*
Expand All @@ -120,6 +128,7 @@ class RenewalHandler {
* @param Environment $environment The environment.
* @param Settings $settings The Settings.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
* @param FundingSourceRenderer $funding_source_renderer The funding source renderer.
*/
public function __construct(
LoggerInterface $logger,
Expand All @@ -130,7 +139,8 @@ public function __construct(
PayerFactory $payer_factory,
Environment $environment,
Settings $settings,
AuthorizedPaymentsProcessor $authorized_payments_processor
AuthorizedPaymentsProcessor $authorized_payments_processor,
FundingSourceRenderer $funding_source_renderer
) {

$this->logger = $logger;
Expand All @@ -142,6 +152,7 @@ public function __construct(
$this->environment = $environment;
$this->settings = $settings;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->funding_source_renderer = $funding_source_renderer;
}

/**
Expand Down Expand Up @@ -410,6 +421,11 @@ private function handle_paypal_order( \WC_Order $wc_order, Order $order ): void
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );

$payment_source = $order->payment_source();
if ( $payment_source instanceof PaymentSource ) {
$this->update_payment_source( $payment_source, $wc_order );
}

$subscriptions = wcs_get_subscriptions_for_order( $wc_order->get_id(), array( 'order_type' => 'any' ) );
foreach ( $subscriptions as $id => $subscription ) {
$subscription->update_meta_data( 'ppcp_previous_transaction_reference', $transaction_id );
Expand Down Expand Up @@ -463,4 +479,29 @@ private function card_payment_source( string $token, WC_Order $wc_order ): Payme
(object) $properties
);
}

/**
* Updates the payment source name to the one really used for the payment.
*
* @param PaymentSource $payment_source
* @param \WC_Order $wc_order
* @return void
*/
private function update_payment_source( PaymentSource $payment_source, \WC_Order $wc_order ): void {
if ( ! $payment_source->name() ) {
return;
}
try {
$wc_order->set_payment_method_title( $this->funding_source_renderer->render_name( $payment_source->name() ) );
$wc_order->save();
} catch ( \Exception $e ) {
$this->logger->error(
sprintf(
'Failed to update payment source to "%1$s" on order %2$d',
$payment_source->name(),
$wc_order->get_id()
)
);
}
}
}

0 comments on commit 6a99a9b

Please sign in to comment.