Fix update payment source on WC Order subscription renewals.

This commit is contained in:
Pedro Silva 2024-02-05 16:30:19 +00:00
parent 977f9836e2
commit 6a99a9befc
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
2 changed files with 45 additions and 2 deletions

View file

@ -27,6 +27,7 @@ return array(
$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 @@ return array(
$payer_factory,
$environment,
$settings,
$authorized_payments_processor
$authorized_payments_processor,
$funding_source_renderer
);
},
'wc-subscriptions.repository.payment-token' => static function ( ContainerInterface $container ): PaymentTokenRepository {

View file

@ -28,6 +28,7 @@ use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
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 @@ class RenewalHandler {
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 @@ class RenewalHandler {
$this->environment = $environment;
$this->settings = $settings;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->funding_source_renderer = $funding_source_renderer;
}
/**
@ -410,6 +421,11 @@ class RenewalHandler {
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 @@ class RenewalHandler {
(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()
)
);
}
}
}