mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Introduce vaulting payment token created webhook (wip)
This commit is contained in:
parent
1092298038
commit
2ba986b342
5 changed files with 177 additions and 2 deletions
|
@ -234,7 +234,16 @@ return array(
|
|||
$payments_endpoint = $container->get( 'api.endpoint.payments' );
|
||||
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
||||
$notice = $container->get( 'wcgateway.notice.authorize-order-action' );
|
||||
return new AuthorizedPaymentsProcessor( $order_endpoint, $payments_endpoint, $logger, $notice );
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
$subscription_helper = $container->get( 'subscription.helper' );
|
||||
return new AuthorizedPaymentsProcessor(
|
||||
$order_endpoint,
|
||||
$payments_endpoint,
|
||||
$logger,
|
||||
$notice,
|
||||
$settings,
|
||||
$subscription_helper
|
||||
);
|
||||
},
|
||||
'wcgateway.admin.render-authorize-action' => static function ( ContainerInterface $container ): RenderAuthorizeAction {
|
||||
$column = $container->get( 'wcgateway.admin.orders-payment-status-column' );
|
||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
|
||||
|
||||
use Exception;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
||||
|
@ -18,6 +19,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
|
||||
|
||||
|
@ -72,6 +74,18 @@ class AuthorizedPaymentsProcessor {
|
|||
*/
|
||||
private $notice;
|
||||
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var SubscriptionHelper
|
||||
*/
|
||||
private $subscription_helper;
|
||||
|
||||
/**
|
||||
* AuthorizedPaymentsProcessor constructor.
|
||||
*
|
||||
|
@ -79,18 +93,23 @@ class AuthorizedPaymentsProcessor {
|
|||
* @param PaymentsEndpoint $payments_endpoint The Payments endpoint.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param AuthorizeOrderActionNotice $notice The notice.
|
||||
* @param ContainerInterface $config The settings.
|
||||
*/
|
||||
public function __construct(
|
||||
OrderEndpoint $order_endpoint,
|
||||
PaymentsEndpoint $payments_endpoint,
|
||||
LoggerInterface $logger,
|
||||
AuthorizeOrderActionNotice $notice
|
||||
AuthorizeOrderActionNotice $notice,
|
||||
ContainerInterface $config,
|
||||
SubscriptionHelper $subscription_helper
|
||||
) {
|
||||
|
||||
$this->order_endpoint = $order_endpoint;
|
||||
$this->payments_endpoint = $payments_endpoint;
|
||||
$this->logger = $logger;
|
||||
$this->notice = $notice;
|
||||
$this->config = $config;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -188,6 +207,23 @@ class AuthorizedPaymentsProcessor {
|
|||
return false;
|
||||
}
|
||||
|
||||
public function capture_authorized_payments_for_customer(int $customer_id) {
|
||||
|
||||
$wc_orders = wc_get_orders(array(
|
||||
'customer_id' => $customer_id,
|
||||
'status' => array('wc-on-hold'),
|
||||
'limit' => -1,
|
||||
));
|
||||
|
||||
if ($this->config->has('intent') && strtoupper((string)$this->config->get('intent')) === 'CAPTURE') {
|
||||
foreach ($wc_orders as $wc_order) {
|
||||
if( $this->subscription_helper->has_subscription( $wc_order->get_id() ) ) {
|
||||
$this->capture_authorized_payment($wc_order);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the notice for a status.
|
||||
*
|
||||
|
|
|
@ -23,6 +23,8 @@ use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureCompleted;
|
|||
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureReversed;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Handler\VaultCreditCardCreated;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Handler\VaultPaymentTokenCreated;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Status\Assets\WebhooksStatusPageAssets;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Status\WebhookSimulation;
|
||||
|
||||
|
@ -67,12 +69,15 @@ return array(
|
|||
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
||||
$prefix = $container->get( 'api.prefix' );
|
||||
$order_endpoint = $container->get( 'api.endpoint.order' );
|
||||
$authorized_payments_processor = $container->get('wcgateway.processor.authorized-payments');
|
||||
return array(
|
||||
new CheckoutOrderApproved( $logger, $prefix, $order_endpoint ),
|
||||
new CheckoutOrderCompleted( $logger, $prefix ),
|
||||
new PaymentCaptureRefunded( $logger, $prefix ),
|
||||
new PaymentCaptureReversed( $logger, $prefix ),
|
||||
new PaymentCaptureCompleted( $logger, $prefix, $order_endpoint ),
|
||||
new VaultPaymentTokenCreated($logger, $prefix, $authorized_payments_processor),
|
||||
new VaultCreditCardCreated($logger, $prefix),
|
||||
);
|
||||
},
|
||||
|
||||
|
|
55
modules/ppcp-webhooks/src/Handler/VaultCreditCardCreated.php
Normal file
55
modules/ppcp-webhooks/src/Handler/VaultCreditCardCreated.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
/**
|
||||
* Handles the Webhook VAULT.PAYMENT-TOKEN.CREATED
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Webhooks\Handler
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class VaultCreditCardCreated implements RequestHandler
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
public function __construct(LoggerInterface $logger, string $prefix)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function event_types(): array
|
||||
{
|
||||
return array(
|
||||
'VAULT.CREDIT-CARD.CREATED',
|
||||
);
|
||||
}
|
||||
|
||||
public function responsible_for_request(\WP_REST_Request $request): bool
|
||||
{
|
||||
return in_array( $request['event_type'], $this->event_types(), true );
|
||||
}
|
||||
|
||||
public function handle_request(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$message = 'VAULT.CREDIT-CARD.CREATED received.';
|
||||
$this->logger->log('info', $message);
|
||||
$response = array(
|
||||
'success' => true,
|
||||
'message' => $message,
|
||||
);
|
||||
|
||||
return rest_ensure_response($response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
/**
|
||||
* Handles the Webhook VAULT.PAYMENT-TOKEN.CREATED
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Webhooks\Handler
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||
|
||||
class VaultPaymentTokenCreated implements RequestHandler
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* @var AuthorizedPaymentsProcessor
|
||||
*/
|
||||
protected $authorized_payments_processor;
|
||||
|
||||
public function __construct(LoggerInterface $logger, string $prefix, AuthorizedPaymentsProcessor $authorized_payments_processor)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
$this->authorized_payments_processor = $authorized_payments_processor;
|
||||
}
|
||||
|
||||
public function event_types(): array
|
||||
{
|
||||
return array(
|
||||
'VAULT.PAYMENT-TOKEN.CREATED',
|
||||
);
|
||||
}
|
||||
|
||||
public function responsible_for_request(\WP_REST_Request $request): bool
|
||||
{
|
||||
return in_array( $request['event_type'], $this->event_types(), true );
|
||||
}
|
||||
|
||||
public function handle_request(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = array( 'success' => false );
|
||||
$webhook_id = (string) ( $request['id'] ?? '' );
|
||||
|
||||
$customer_id = $resource['customer_id'] ?? '';
|
||||
if(!$customer_id) {
|
||||
$message = sprintf( 'No customer id for webhook event %s was found.', $webhook_id );
|
||||
$this->logger->warning( $message, array( 'request' => $request ) );
|
||||
$response['message'] = $message;
|
||||
return new \WP_REST_Response( $response );
|
||||
}
|
||||
|
||||
$customer_id_parts = explode('-', $customer_id);
|
||||
$this->authorized_payments_processor->capture_authorized_payments_for_customer((int) end($customer_id_parts));
|
||||
|
||||
$response['success'] = true;
|
||||
return rest_ensure_response($response);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue