add vault token to orders and check them for getting transaction id

This commit is contained in:
Daniel Hüsken 2025-02-26 09:50:40 +01:00
parent c21bbf38f1
commit 85bcd0a454
No known key found for this signature in database
GPG key ID: 9F732DA37FA709E8
4 changed files with 30 additions and 17 deletions

View file

@ -430,6 +430,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
foreach ( $tokens as $token ) {
if ( $token->get_id() === (int) $card_payment_token_for_free_trial ) {
$wc_order->payment_complete();
$wc_order->add_payment_token( $token );
return $this->handle_payment_success( $wc_order );
}
}
@ -475,6 +476,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$order = $this->order_endpoint->order( $create_order->id );
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
$wc_order->add_payment_token( $token );
if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );

View file

@ -285,11 +285,12 @@ class SubscriptionHelper {
* Returns previous order transaction from the given subscription.
*
* @param WC_Subscription $subscription WooCommerce Subscription.
* @param string $vault_token_id Vault token id.
* @return string
*/
public function previous_transaction( WC_Subscription $subscription ): string {
public function previous_transaction( WC_Subscription $subscription, string $vault_token_id ): string {
$orders = $subscription->get_related_orders( 'ids', array( 'parent', 'renewal' ) );
if ( ! $orders ) {
if ( ! $orders || ! $vault_token_id ) {
return '';
}
@ -308,11 +309,15 @@ class SubscriptionHelper {
&& $current_order->get_payment_method() === $order->get_payment_method()
) {
$transaction_id = $order->get_transaction_id();
if ( $transaction_id ) {
$tokens = $order->get_payment_tokens();
foreach ( $tokens as $token ) {
$wc_token = \WC_Payment_Tokens::get( $token );
if ( $transaction_id && $wc_token->get_token() === $vault_token_id ) {
return $transaction_id;
}
}
}
}
return '';
}

View file

@ -335,6 +335,7 @@ class RenewalHandler {
$last_token = end( $wc_tokens );
if ( $last_token ) {
$payment_source = $this->card_payment_source( $last_token->get_token(), $wc_order );
$wc_order->add_payment_token( $last_token );
}
}
@ -543,24 +544,20 @@ class RenewalHandler {
private function card_payment_source( string $token, WC_Order $wc_order ): PaymentSource {
$properties = array(
'vault_id' => $token,
'stored_credential' => array(
'payment_initiator' => 'MERCHANT',
'payment_type' => 'RECURRING',
'usage' => 'SUBSEQUENT',
),
);
$subscriptions = wcs_get_subscriptions_for_renewal_order( $wc_order );
$subscription = end( $subscriptions );
if ( $subscription ) {
$transaction = $this->subscription_helper->previous_transaction( $subscription );
$transaction = $this->subscription_helper->previous_transaction( $subscription, $token );
if ( $transaction ) {
$properties['stored_credential'] = array(
'payment_initiator' => 'MERCHANT',
'payment_type' => 'RECURRING',
'usage' => 'SUBSEQUENT',
'previous_transaction_reference' => $transaction,
);
} else {
$this->logger->debug( sprintf( 'Previous transaction not found for subscription %s', $subscription->get_id() ) );
$properties['stored_credential']['previous_transaction_reference'] = $transaction;
}
} else {
$this->logger->debug( sprintf( 'Subscription not found for renewal order %s', $wc_order->get_id() ) );
}
return new PaymentSource(

View file

@ -24,16 +24,25 @@ class SubscriptionHelperTest extends TestCase
]
);
$token = Mockery::mock( \WC_Payment_Token::class);
$token->shouldReceive('get_token')->andReturn('token12345');
$tokens = Mockery::mock( 'overload:' . \WC_Payment_Tokens::class );
$tokens->shouldReceive('get')->andReturn( $token );
$wc_order = Mockery::mock(WC_Order::class);
$wc_order->shouldReceive('get_status')->andReturn('processing');
$wc_order->shouldReceive('get_transaction_id')->andReturn('ABC123');
$wc_order->shouldReceive('get_payment_method')->andReturn(CreditCardGateway::ID);
$wc_order->shouldReceive('get_payment_tokens')->andReturn(['token12345']);
when('wc_get_order')->justReturn($wc_order);
$this->assertSame(
'ABC123',
(new SubscriptionHelper())->previous_transaction($subscription)
(new SubscriptionHelper())->previous_transaction($subscription, 'token12345')
);
}
}