From 6a99a9befcd0e5cd1133b0701b645d02e8940e12 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Mon, 5 Feb 2024 16:30:19 +0000
Subject: [PATCH] Fix update payment source on WC Order subscription renewals.
---
modules/ppcp-wc-subscriptions/services.php | 4 +-
.../src/RenewalHandler.php | 43 ++++++++++++++++++-
2 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/modules/ppcp-wc-subscriptions/services.php b/modules/ppcp-wc-subscriptions/services.php
index 86599b3f2..e9b324b07 100644
--- a/modules/ppcp-wc-subscriptions/services.php
+++ b/modules/ppcp-wc-subscriptions/services.php
@@ -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,
@@ -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 {
diff --git a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php
index a4ce028ee..06d223c35 100644
--- a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php
+++ b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php
@@ -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;
@@ -108,6 +109,13 @@ class RenewalHandler {
*/
protected $authorized_payments_processor;
+ /**
+ * The funding source renderer.
+ *
+ * @var FundingSourceRenderer
+ */
+ protected $funding_source_renderer;
+
/**
* RenewalHandler constructor.
*
@@ -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,
@@ -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;
@@ -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;
}
/**
@@ -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 );
@@ -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()
+ )
+ );
+ }
+ }
}