mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Check if current payment exists in payment tokens
This commit is contained in:
parent
7109f969f6
commit
632c928ac3
4 changed files with 62 additions and 3 deletions
|
@ -132,7 +132,7 @@ class PurchaseUnitFactory {
|
|||
$description,
|
||||
$payee,
|
||||
$custom_id,
|
||||
$invoice_id,
|
||||
$invoice_id . $retry,
|
||||
$soft_descriptor
|
||||
);
|
||||
return apply_filters(
|
||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\Vaulting;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
|
@ -121,6 +122,26 @@ class PaymentTokenRepository {
|
|||
return $this->token_contains_source( $tokens, 'paypal' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tokens has the given payment source.
|
||||
*
|
||||
* @param PaymentToken[] $tokens The tokens.
|
||||
* @param PaymentSource|null $payment_source The payment source.
|
||||
* @return bool Whether tokens contains payment source or not.
|
||||
*/
|
||||
public function tokens_contains_payment_source( array $tokens, PaymentSource $payment_source ): bool {
|
||||
|
||||
if ( $this->tokens_contains_card( $tokens ) ) {
|
||||
foreach ( $tokens as $token ) {
|
||||
if ( $payment_source->card()->last_digits() === $token->source()->card->last_digits ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch PaymentToken from PayPal for a user.
|
||||
*
|
||||
|
|
|
@ -173,10 +173,12 @@ trait ProcessPaymentTrait {
|
|||
if ( $this->order_processor->process( $wc_order ) ) {
|
||||
|
||||
if ( $this->subscription_helper->has_subscription( $order_id ) ) {
|
||||
$this->logger->info( "Trying to save payment for subscription parent order #{$order_id}." );
|
||||
$this->logger->info( "Checking if payment for subscription parent order #{$order_id} is saved." );
|
||||
|
||||
$tokens = $this->payment_token_repository->all_for_user_id( $wc_order->get_customer_id() );
|
||||
if ( $tokens ) {
|
||||
$current_payment_source = $this->session_handler->order()->payment_source();
|
||||
if ( $tokens && $this->payment_token_repository->tokens_contains_payment_source($tokens, $current_payment_source) ) {
|
||||
|
||||
$this->logger->info( "Payment for subscription parent order #{$order_id} was saved correctly." );
|
||||
|
||||
if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
|
||||
|
|
36
tests/PHPUnit/Vaulting/PaymentTokenRepositoryTest.php
Normal file
36
tests/PHPUnit/Vaulting/PaymentTokenRepositoryTest.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace PHPUnit\Vaulting;
|
||||
|
||||
use Mockery;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
use WooCommerce\PayPalCommerce\TestCase;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
|
||||
class PaymentTokenRepositoryTest extends TestCase
|
||||
{
|
||||
|
||||
public function testTokensContainsCardPaymentSource()
|
||||
{
|
||||
$paymentSource = Mockery::mock(PaymentSource::class);
|
||||
$paymentSource->shouldReceive('card->last_digits')->andReturn('1234');
|
||||
|
||||
$token = Mockery::mock(PaymentToken::class);
|
||||
$source = (object)[
|
||||
'card' => (object)[
|
||||
'last_digits' => '1234',
|
||||
],
|
||||
];
|
||||
$token->shouldReceive('source')->andReturn($source);
|
||||
$tokens = [$token];
|
||||
|
||||
$factory = Mockery::mock(PaymentTokenFactory::class);
|
||||
$endpoint = Mockery::mock(PaymentTokenEndpoint::class);
|
||||
$testee = new PaymentTokenRepository($factory, $endpoint);
|
||||
|
||||
self::assertTrue($testee->tokens_contains_payment_source($tokens, $paymentSource));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue