PCP-39 add capture downloads only immediatly option

This commit is contained in:
David Remer 2020-08-25 13:55:07 +03:00
parent abd4c171dc
commit 607a247133
4 changed files with 74 additions and 3 deletions

View file

@ -160,6 +160,15 @@ const groupToggleSelect = (selector, group) => {
} }
] ]
); );
groupToggleSelect(
'#ppcp-intent',
[
{
value:'authorize',
selector:'#field-capture_for_virtual_only'
}
]
);
groupToggleSelect( groupToggleSelect(
'#ppcp-message_cart_layout', '#ppcp-message_cart_layout',
[ [

View file

@ -104,13 +104,17 @@ return [
$paymentsEndpoint = $container->get('api.endpoint.payments'); $paymentsEndpoint = $container->get('api.endpoint.payments');
$orderFactory = $container->get('api.factory.order'); $orderFactory = $container->get('api.factory.order');
$threeDsecure = $container->get('button.helper.three-d-secure'); $threeDsecure = $container->get('button.helper.three-d-secure');
$authorizedPaymentsProcessor = $container->get('wcgateway.processor.authorized-payments');
$settings = $container->get('wcgateway.settings');
return new OrderProcessor( return new OrderProcessor(
$sessionHandler, $sessionHandler,
$cartRepository, $cartRepository,
$orderEndpoint, $orderEndpoint,
$paymentsEndpoint, $paymentsEndpoint,
$orderFactory, $orderFactory,
$threeDsecure $threeDsecure,
$authorizedPaymentsProcessor,
$settings
); );
}, },
'wcgateway.processor.authorized-payments' => static function (ContainerInterface $container): AuthorizedPaymentsProcessor { 'wcgateway.processor.authorized-payments' => static function (ContainerInterface $container): AuthorizedPaymentsProcessor {
@ -340,6 +344,23 @@ return [
'requirements' => [], 'requirements' => [],
'gateway' => 'all', 'gateway' => 'all',
], ],
'capture_for_virtual_only' => [
'title' => __('Capture Virtual-Only Orders ', 'woocommerce-paypal-commerce-gateway'),
'type' => 'checkbox',
'default' => false,
'desc_tip' => true,
'description' => __(
'If the order contains exclusively virtual items, enable this to immediately capture, rather than authorize, the transaction.',
'woocommerce-paypal-commerce-gateway'
),
'label' => __('Capture Virtual-Only Orders', 'woocommerce-paypal-commerce-gateway'),
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
'requirements' => [],
'gateway' => 'all',
],
'payee_preferred' => [ 'payee_preferred' => [
'title' => __('Instant Payments ', 'woocommerce-paypal-commerce-gateway'), 'title' => __('Instant Payments ', 'woocommerce-paypal-commerce-gateway'),
'type' => 'checkbox', 'type' => 'checkbox',

View file

@ -109,7 +109,8 @@ class AuthorizedPaymentsProcessor
return array_filter( return array_filter(
$authorizations, $authorizations,
static function (Authorization $authorization): bool { static function (Authorization $authorization): bool {
return $authorization->status()->is(AuthorizationStatus::CREATED); return $authorization->status()->is(AuthorizationStatus::CREATED)
|| $authorization->status()->is(AuthorizationStatus::PENDING);
} }
); );
} }

View file

@ -13,6 +13,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure; use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
class OrderProcessor class OrderProcessor
{ {
@ -22,6 +23,8 @@ class OrderProcessor
private $paymentsEndpoint; private $paymentsEndpoint;
private $orderFactory; private $orderFactory;
private $threedSecure; private $threedSecure;
private $authorizedPaymentsProcessor;
private $settings;
private $lastError = ''; private $lastError = '';
@ -31,7 +34,9 @@ class OrderProcessor
OrderEndpoint $orderEndpoint, OrderEndpoint $orderEndpoint,
PaymentsEndpoint $paymentsEndpoint, PaymentsEndpoint $paymentsEndpoint,
OrderFactory $orderFactory, OrderFactory $orderFactory,
ThreeDSecure $threedSecure ThreeDSecure $threedSecure,
AuthorizedPaymentsProcessor $authorizedPaymentsProcessor,
Settings $settings
) { ) {
$this->sessionHandler = $sessionHandler; $this->sessionHandler = $sessionHandler;
@ -40,6 +45,8 @@ class OrderProcessor
$this->paymentsEndpoint = $paymentsEndpoint; $this->paymentsEndpoint = $paymentsEndpoint;
$this->orderFactory = $orderFactory; $this->orderFactory = $orderFactory;
$this->threedSecure = $threedSecure; $this->threedSecure = $threedSecure;
$this->authorizedPaymentsProcessor = $authorizedPaymentsProcessor;
$this->settings = $settings;
} }
public function process(\WC_Order $wcOrder, \WooCommerce $woocommerce): bool public function process(\WC_Order $wcOrder, \WooCommerce $woocommerce): bool
@ -84,12 +91,45 @@ class OrderProcessor
__('Payment received.', 'woocommerce-paypal-commerce-gateway') __('Payment received.', 'woocommerce-paypal-commerce-gateway')
); );
} }
if ($this->captureAuthorizedDownloads($order) && $this->authorizedPaymentsProcessor->process($wcOrder)) {
$wcOrder->add_order_note(
__('Payment successfully captured.', 'woocommerce-paypal-commerce-gateway')
);
$wcOrder->update_meta_data(PayPalGateway::CAPTURED_META_KEY, 'true');
$wcOrder->update_status('processing');
}
$woocommerce->cart->empty_cart(); $woocommerce->cart->empty_cart();
$this->sessionHandler->destroySessionData(); $this->sessionHandler->destroySessionData();
$this->lastError = ''; $this->lastError = '';
return true; return true;
} }
private function captureAuthorizedDownloads(Order $order): bool
{
if (! $this->settings->has('capture_for_virtual_only') || ! $this->settings->get('capture_for_virtual_only')) {
return false;
}
if ($order->intent() === 'CAPTURE') {
return false;
}
/**
* We fetch the order again as the authorize endpoint (from which the Order derives)
* drops the item's category, making it impossible to check, if purchase units contain
* physical goods.
*/
$order = $this->orderEndpoint->order($order->id());
foreach ($order->purchaseUnits() as $unit) {
if ($unit->containsPhysicalGoodsItems()) {
return false;
}
}
return true;
}
public function lastError(): string public function lastError(): string
{ {