mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-31 04:58:28 +08:00
Add API for easy programmatic capture
This commit is contained in:
parent
b62072c537
commit
d40d01a4f4
9 changed files with 577 additions and 58 deletions
46
api/order-functions.php
Normal file
46
api/order-functions.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* The API for operations with orders.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\Api
|
||||||
|
*
|
||||||
|
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\Api;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use RuntimeException;
|
||||||
|
use WC_Order;
|
||||||
|
use WooCommerce\PayPalCommerce\PPCP;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Captures the PayPal order.
|
||||||
|
*
|
||||||
|
* @param WC_Order $wc_order The WC order.
|
||||||
|
* @throws InvalidArgumentException When the order cannot be captured.
|
||||||
|
* @throws Exception When the operation fails.
|
||||||
|
*/
|
||||||
|
function ppcp_capture_order( WC_Order $wc_order ): void {
|
||||||
|
$intent = strtoupper( (string) $wc_order->get_meta( PayPalGateway::INTENT_META_KEY ) );
|
||||||
|
|
||||||
|
if ( $intent !== 'AUTHORIZE' ) {
|
||||||
|
throw new InvalidArgumentException( 'Only orders with "authorize" intent can be captured.' );
|
||||||
|
}
|
||||||
|
$captured = wc_string_to_bool( $wc_order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY ) );
|
||||||
|
if ( $captured ) {
|
||||||
|
throw new InvalidArgumentException( 'The order is already captured.' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$authorized_payment_processor = PPCP::container()->get( 'wcgateway.processor.authorized-payments' );
|
||||||
|
assert( $authorized_payment_processor instanceof AuthorizedPaymentsProcessor );
|
||||||
|
|
||||||
|
if ( ! $authorized_payment_processor->capture_authorized_payment( $wc_order ) ) {
|
||||||
|
throw new RuntimeException( 'Capture failed.' );
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,7 +30,10 @@
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"WooCommerce\\PayPalCommerce\\": "src",
|
"WooCommerce\\PayPalCommerce\\": "src",
|
||||||
"WooCommerce\\PayPalCommerce\\Vendor\\": "lib/packages/"
|
"WooCommerce\\PayPalCommerce\\Vendor\\": "lib/packages/"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"api/order-functions.php"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
483
composer.lock
generated
483
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -42,6 +42,7 @@
|
||||||
</rule>
|
</rule>
|
||||||
|
|
||||||
<arg name="extensions" value="php"/>
|
<arg name="extensions" value="php"/>
|
||||||
|
<file>api</file>
|
||||||
<file>src</file>
|
<file>src</file>
|
||||||
<file>modules</file>
|
<file>modules</file>
|
||||||
<file>woocommerce-paypal-payments.php</file>
|
<file>woocommerce-paypal-payments.php</file>
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
errorBaseline="psalm-baseline.xml"
|
errorBaseline="psalm-baseline.xml"
|
||||||
>
|
>
|
||||||
<projectFiles>
|
<projectFiles>
|
||||||
|
<directory name="api"/>
|
||||||
<directory name="src"/>
|
<directory name="src"/>
|
||||||
<directory name="modules" />
|
<directory name="modules" />
|
||||||
<file name="bootstrap.php" />
|
<file name="bootstrap.php" />
|
||||||
|
|
92
tests/PHPUnit/Api/OrderCaptureTest.php
Normal file
92
tests/PHPUnit/Api/OrderCaptureTest.php
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\Api;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Mockery;
|
||||||
|
use RuntimeException;
|
||||||
|
use WC_Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ModularTestCase;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||||
|
|
||||||
|
class OrderCaptureTest extends ModularTestCase
|
||||||
|
{
|
||||||
|
private $authorizedPaymentProcessor;
|
||||||
|
|
||||||
|
public function setUp(): void {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->authorizedPaymentProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
|
||||||
|
|
||||||
|
$this->bootstrapModule([
|
||||||
|
'wcgateway.processor.authorized-payments' => function () {
|
||||||
|
return $this->authorizedPaymentProcessor;
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSuccess(): void {
|
||||||
|
$wcOrder = Mockery::mock(WC_Order::class);
|
||||||
|
$wcOrder->expects('get_meta')
|
||||||
|
->with(PayPalGateway::INTENT_META_KEY)
|
||||||
|
->andReturn('AUTHORIZE');
|
||||||
|
$wcOrder->expects('get_meta')
|
||||||
|
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
|
||||||
|
->andReturn(false);
|
||||||
|
|
||||||
|
$this->authorizedPaymentProcessor
|
||||||
|
->expects('capture_authorized_payment')
|
||||||
|
->andReturnTrue()
|
||||||
|
->once();
|
||||||
|
|
||||||
|
ppcp_capture_order($wcOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFailure(): void {
|
||||||
|
$wcOrder = Mockery::mock(WC_Order::class);
|
||||||
|
$wcOrder->expects('get_meta')
|
||||||
|
->with(PayPalGateway::INTENT_META_KEY)
|
||||||
|
->andReturn('AUTHORIZE');
|
||||||
|
$wcOrder->expects('get_meta')
|
||||||
|
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
|
||||||
|
->andReturn(false);
|
||||||
|
|
||||||
|
$this->authorizedPaymentProcessor
|
||||||
|
->expects('capture_authorized_payment')
|
||||||
|
->andReturnFalse()
|
||||||
|
->once();
|
||||||
|
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
|
||||||
|
ppcp_capture_order($wcOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNotAuthorize(): void {
|
||||||
|
$wcOrder = Mockery::mock(WC_Order::class);
|
||||||
|
$wcOrder->shouldReceive('get_meta')
|
||||||
|
->with(PayPalGateway::INTENT_META_KEY)
|
||||||
|
->andReturn('CAPTURE');
|
||||||
|
$wcOrder->shouldReceive('get_meta')
|
||||||
|
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
|
||||||
|
->andReturn(false);
|
||||||
|
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
|
||||||
|
ppcp_capture_order($wcOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAlreadyCaptured(): void {
|
||||||
|
$wcOrder = Mockery::mock(WC_Order::class);
|
||||||
|
$wcOrder->shouldReceive('get_meta')
|
||||||
|
->with(PayPalGateway::INTENT_META_KEY)
|
||||||
|
->andReturn('CAPTURE');
|
||||||
|
$wcOrder->shouldReceive('get_meta')
|
||||||
|
->with(AuthorizedPaymentsProcessor::CAPTURED_META_KEY)
|
||||||
|
->andReturn(true);
|
||||||
|
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
|
||||||
|
ppcp_capture_order($wcOrder);
|
||||||
|
}
|
||||||
|
}
|
|
@ -47,7 +47,7 @@ class IdentityTokenTest extends TestCase
|
||||||
public function testGenerateForCustomerReturnsToken()
|
public function testGenerateForCustomerReturnsToken()
|
||||||
{
|
{
|
||||||
$id = 1;
|
$id = 1;
|
||||||
define( 'PPCP_FLAG_SUBSCRIPTION', true );
|
!defined('PPCP_FLAG_SUBSCRIPTION') && define('PPCP_FLAG_SUBSCRIPTION', true);
|
||||||
$token = Mockery::mock(Token::class);
|
$token = Mockery::mock(Token::class);
|
||||||
$token
|
$token
|
||||||
->expects('token')->andReturn('bearer');
|
->expects('token')->andReturn('bearer');
|
||||||
|
|
|
@ -82,6 +82,8 @@ class ModularTestCase extends TestCase
|
||||||
$bootstrap = require ("$rootDir/bootstrap.php");
|
$bootstrap = require ("$rootDir/bootstrap.php");
|
||||||
$appContainer = $bootstrap($rootDir, [], [$module]);
|
$appContainer = $bootstrap($rootDir, [], [$module]);
|
||||||
|
|
||||||
|
PPCP::init($appContainer);
|
||||||
|
|
||||||
return $appContainer;
|
return $appContainer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,9 @@ class TestCase extends \PHPUnit\Framework\TestCase
|
||||||
when('wc_print_r')->alias(function ($value, bool $return = false) {
|
when('wc_print_r')->alias(function ($value, bool $return = false) {
|
||||||
return print_r($value, $return);
|
return print_r($value, $return);
|
||||||
});
|
});
|
||||||
|
when('wc_string_to_bool')->alias(function ($string) {
|
||||||
|
return is_bool( $string ) ? $string : ( 'yes' === strtolower( $string ) || 1 === $string || 'true' === strtolower( $string ) || '1' === $string );
|
||||||
|
});
|
||||||
when('get_plugin_data')->justReturn(['Version' => '1.0']);
|
when('get_plugin_data')->justReturn(['Version' => '1.0']);
|
||||||
when('plugin_basename')->justReturn('woocommerce-paypal-payments/woocommerce-paypal-payments.php');
|
when('plugin_basename')->justReturn('woocommerce-paypal-payments/woocommerce-paypal-payments.php');
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue