mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +08:00
Add unit tests for vaulted card handler
This commit is contained in:
parent
0f47b2ef37
commit
0e24b1d0f8
3 changed files with 155 additions and 5 deletions
|
@ -118,7 +118,8 @@ class VaultedCreditCardHandler
|
||||||
|
|
||||||
public function handle_payment(
|
public function handle_payment(
|
||||||
string $saved_credit_card,
|
string $saved_credit_card,
|
||||||
WC_Order $wc_order
|
WC_Order $wc_order,
|
||||||
|
WC_Customer $customer
|
||||||
): WC_Order {
|
): WC_Order {
|
||||||
|
|
||||||
$change_payment = filter_input( INPUT_POST, 'woocommerce_change_payment', FILTER_SANITIZE_STRING );
|
$change_payment = filter_input( INPUT_POST, 'woocommerce_change_payment', FILTER_SANITIZE_STRING );
|
||||||
|
@ -132,9 +133,7 @@ class VaultedCreditCardHandler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$user_id = (int) $wc_order->get_customer_id();
|
$tokens = $this->payment_token_repository->all_for_user_id( $wc_order->get_customer_id() );
|
||||||
$customer = new WC_Customer( $user_id );
|
|
||||||
$tokens = $this->payment_token_repository->all_for_user_id( (int) $customer->get_id() );
|
|
||||||
$selected_token = null;
|
$selected_token = null;
|
||||||
foreach ( $tokens as $token ) {
|
foreach ( $tokens as $token ) {
|
||||||
if ( $token->id() === $saved_credit_card ) {
|
if ( $token->id() === $saved_credit_card ) {
|
||||||
|
@ -148,6 +147,7 @@ class VaultedCreditCardHandler
|
||||||
|
|
||||||
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
||||||
$payer = $this->payer_factory->from_customer( $customer );
|
$payer = $this->payer_factory->from_customer( $customer );
|
||||||
|
|
||||||
$shipping_preference = $this->shipping_preference_factory->from_state(
|
$shipping_preference = $this->shipping_preference_factory->from_state(
|
||||||
$purchase_unit,
|
$purchase_unit,
|
||||||
''
|
''
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use WC_Customer;
|
||||||
use WC_Order;
|
use WC_Order;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
|
@ -360,9 +361,12 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
$saved_credit_card = filter_input( INPUT_POST, 'saved_credit_card', FILTER_SANITIZE_STRING );
|
$saved_credit_card = filter_input( INPUT_POST, 'saved_credit_card', FILTER_SANITIZE_STRING );
|
||||||
if($saved_credit_card) {
|
if($saved_credit_card) {
|
||||||
try {
|
try {
|
||||||
|
$customer = new WC_Customer( $wc_order->get_customer_id() );
|
||||||
|
|
||||||
$wc_order = $this->vaulted_credit_card_handler->handle_payment(
|
$wc_order = $this->vaulted_credit_card_handler->handle_payment(
|
||||||
$saved_credit_card,
|
$saved_credit_card,
|
||||||
$wc_order
|
$wc_order,
|
||||||
|
$customer
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->handle_payment_success( $wc_order );
|
return $this->handle_payment_success( $wc_order );
|
||||||
|
|
146
tests/PHPUnit/Vaulting/VaultedCreditCardHandlerTest.php
Normal file
146
tests/PHPUnit/Vaulting/VaultedCreditCardHandlerTest.php
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPUnit\Vaulting;
|
||||||
|
|
||||||
|
use Mockery;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use WC_Customer;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payments;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSourceCard;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
||||||
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||||
|
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||||
|
use WooCommerce\PayPalCommerce\TestCase;
|
||||||
|
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||||
|
use WooCommerce\PayPalCommerce\Vaulting\VaultedCreditCardHandler;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||||
|
use function Brain\Monkey\Functions\expect;
|
||||||
|
use function Brain\Monkey\Functions\when;
|
||||||
|
|
||||||
|
class VaultedCreditCardHandlerTest extends TestCase
|
||||||
|
{
|
||||||
|
private $subscriptionHelper;
|
||||||
|
private $paymentTokenRepository;
|
||||||
|
private $purchaseUnitFactory;
|
||||||
|
private $payerFactory;
|
||||||
|
private $shippingPreferenceFactory;
|
||||||
|
private $orderEndpoint;
|
||||||
|
private $environment;
|
||||||
|
private $authorizedPaymentProcessor;
|
||||||
|
private $config;
|
||||||
|
private $testee;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
|
||||||
|
$this->paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class);
|
||||||
|
$this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class);
|
||||||
|
$this->payerFactory = Mockery::mock(PayerFactory::class);
|
||||||
|
$this->shippingPreferenceFactory = Mockery::mock(ShippingPreferenceFactory::class);
|
||||||
|
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||||
|
$this->environment = Mockery::mock(Environment::class);
|
||||||
|
$this->authorizedPaymentProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
$this->config = Mockery::mock(ContainerInterface::class);
|
||||||
|
|
||||||
|
$this->testee = new VaultedCreditCardHandler(
|
||||||
|
$this->subscriptionHelper,
|
||||||
|
$this->paymentTokenRepository,
|
||||||
|
$this->purchaseUnitFactory,
|
||||||
|
$this->payerFactory,
|
||||||
|
$this->shippingPreferenceFactory,
|
||||||
|
$this->orderEndpoint,
|
||||||
|
$this->environment,
|
||||||
|
$this->authorizedPaymentProcessor,
|
||||||
|
$this->config
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandlePaymentChangingPayment()
|
||||||
|
{
|
||||||
|
when('filter_input')->justReturn(1);
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$wcOrder->shouldReceive('get_id')->andReturn(1);
|
||||||
|
$this->subscriptionHelper->shouldReceive('has_subscription')->andReturn(true);
|
||||||
|
$this->subscriptionHelper->shouldReceive('is_subscription_change_payment')->andReturn(true);
|
||||||
|
expect('update_post_meta')->with(1, 'payment_token_id', 'abc123');
|
||||||
|
|
||||||
|
$customer = Mockery::mock(WC_Customer::class);
|
||||||
|
|
||||||
|
$result = $this->testee->handle_payment('abc123', $wcOrder, $customer);
|
||||||
|
$this->assertInstanceOf(\WC_Order::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandlePayment()
|
||||||
|
{
|
||||||
|
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||||
|
$wcOrder->shouldReceive('get_id')->andReturn(1);
|
||||||
|
$wcOrder->shouldReceive('get_customer_id')->andReturn(1);
|
||||||
|
$wcOrder->shouldReceive('update_meta_data')->andReturn(1);
|
||||||
|
$wcOrder->shouldReceive('save')->once();
|
||||||
|
$wcOrder->shouldReceive('payment_complete')->andReturn(true);
|
||||||
|
|
||||||
|
$token = Mockery::mock(PaymentToken::class);
|
||||||
|
$tokenId = 'abc123';
|
||||||
|
$token->shouldReceive('id')->andReturn($tokenId);
|
||||||
|
$this->paymentTokenRepository->shouldReceive('all_for_user_id')
|
||||||
|
->andReturn([$token]);
|
||||||
|
|
||||||
|
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
|
||||||
|
$this->purchaseUnitFactory->shouldReceive('from_wc_order')
|
||||||
|
->andReturn($purchaseUnit);
|
||||||
|
|
||||||
|
$customer = Mockery::mock(WC_Customer::class);
|
||||||
|
|
||||||
|
$payer = Mockery::mock(Payer::class);
|
||||||
|
$this->payerFactory->shouldReceive('from_customer')
|
||||||
|
->andReturn($payer);
|
||||||
|
$this->shippingPreferenceFactory->shouldReceive('from_state')
|
||||||
|
->andReturn('some_preference');
|
||||||
|
|
||||||
|
$order = Mockery::mock(Order::class);
|
||||||
|
$order->shouldReceive('id')->andReturn('1');
|
||||||
|
$order->shouldReceive('intent')->andReturn('CAPTURE');
|
||||||
|
$paymentSource = Mockery::mock(PaymentSource::class);
|
||||||
|
$paymentSourceCard = Mockery::mock(PaymentSourceCard::class);
|
||||||
|
$paymentSource->shouldReceive('card')->andReturn($paymentSourceCard);
|
||||||
|
$order->shouldReceive('payment_source')->andReturn($paymentSource);
|
||||||
|
$orderStatus = Mockery::mock(OrderStatus::class);
|
||||||
|
$orderStatus->shouldReceive('is')->andReturn(true);
|
||||||
|
$order->shouldReceive('status')->andReturn($orderStatus);
|
||||||
|
|
||||||
|
$order->shouldReceive('purchase_units')->andReturn([$purchaseUnit]);
|
||||||
|
$payments = Mockery::mock(Payments::class);
|
||||||
|
$capture = Mockery::mock(Capture::class);
|
||||||
|
$capture->shouldReceive('id')->andReturn('1');
|
||||||
|
$captureStatus = Mockery::mock(CaptureStatus::class);
|
||||||
|
$captureStatus->shouldReceive('details')->andReturn(null);
|
||||||
|
$captureStatus->shouldReceive('name')->andReturn(CaptureStatus::COMPLETED);
|
||||||
|
$capture->shouldReceive('status')->andReturn($captureStatus);
|
||||||
|
$payments->shouldReceive('captures')->andReturn([$capture]);
|
||||||
|
$purchaseUnit->shouldReceive('payments')->andReturn($payments);
|
||||||
|
|
||||||
|
$this->orderEndpoint->shouldReceive('create')
|
||||||
|
->with([$purchaseUnit], 'some_preference', $payer, $token)
|
||||||
|
->andReturn($order);
|
||||||
|
|
||||||
|
$this->environment->shouldReceive('current_environment_is')->andReturn(true);
|
||||||
|
|
||||||
|
$this->config->shouldReceive('has')->andReturn(false);
|
||||||
|
|
||||||
|
$result = $this->testee->handle_payment($tokenId, $wcOrder, $customer);
|
||||||
|
$this->assertInstanceOf(\WC_Order::class, $result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue