mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Move vault v3 renewal logic from filter to renewal handler
This commit is contained in:
parent
6b843bc698
commit
4cc03ccf1d
2 changed files with 56 additions and 45 deletions
|
@ -79,27 +79,6 @@ class SavePaymentMethodsModule implements ModuleInterface {
|
||||||
add_filter(
|
add_filter(
|
||||||
'ppcp_create_order_request_body_data',
|
'ppcp_create_order_request_body_data',
|
||||||
function( array $data, string $payment_method, array $request_data ): array {
|
function( array $data, string $payment_method, array $request_data ): array {
|
||||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
|
||||||
$wc_order_action = wc_clean( wp_unslash( $_POST['wc_order_action'] ?? '' ) );
|
|
||||||
if ( $wc_order_action === 'wcs_process_renewal' ) {
|
|
||||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
|
||||||
$subscription_id = wc_clean( wp_unslash( $_POST['post_ID'] ?? '' ) );
|
|
||||||
$subscription = wcs_get_subscription( (int) $subscription_id );
|
|
||||||
if ( $subscription ) {
|
|
||||||
$customer_id = $subscription->get_customer_id();
|
|
||||||
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
|
|
||||||
foreach ( $wc_tokens as $token ) {
|
|
||||||
$data['payment_source'] = array(
|
|
||||||
'paypal' => array(
|
|
||||||
'vault_id' => $token->get_token(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $payment_method === CreditCardGateway::ID ) {
|
if ( $payment_method === CreditCardGateway::ID ) {
|
||||||
$save_payment_method = $request_data['save_payment_method'] ?? false;
|
$save_payment_method = $request_data['save_payment_method'] ?? false;
|
||||||
if ( $save_payment_method ) {
|
if ( $save_payment_method ) {
|
||||||
|
@ -114,6 +93,10 @@ class SavePaymentMethodsModule implements ModuleInterface {
|
||||||
);
|
);
|
||||||
|
|
||||||
$target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
|
$target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
|
||||||
|
if ( ! $target_customer_id ) {
|
||||||
|
$target_customer_id = get_user_meta( get_current_user_id(), 'ppcp_customer_id', true );
|
||||||
|
}
|
||||||
|
|
||||||
if ( $target_customer_id ) {
|
if ( $target_customer_id ) {
|
||||||
$data['payment_source']['card']['attributes']['customer'] = array(
|
$data['payment_source']['card']['attributes']['customer'] = array(
|
||||||
'id' => $target_customer_id,
|
'id' => $target_customer_id,
|
||||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||||
namespace WooCommerce\PayPalCommerce\WcSubscriptions;
|
namespace WooCommerce\PayPalCommerce\WcSubscriptions;
|
||||||
|
|
||||||
use WC_Subscription;
|
use WC_Subscription;
|
||||||
|
use WC_Payment_Tokens;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||||
|
@ -24,6 +25,7 @@ use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
|
||||||
|
@ -194,6 +196,7 @@ class RenewalHandler {
|
||||||
'renewal'
|
'renewal'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Vault v2.
|
||||||
$token = $this->get_token_for_customer( $customer, $wc_order );
|
$token = $this->get_token_for_customer( $customer, $wc_order );
|
||||||
if ( $token ) {
|
if ( $token ) {
|
||||||
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
|
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
|
||||||
|
@ -267,20 +270,56 @@ class RenewalHandler {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$order = $this->order_endpoint->create(
|
// Vault v3.
|
||||||
array( $purchase_unit ),
|
$payment_source = null;
|
||||||
$shipping_preference,
|
if ( $wc_order->get_payment_method() === PayPalGateway::ID ) {
|
||||||
$payer
|
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), PayPalGateway::ID );
|
||||||
);
|
foreach ( $wc_tokens as $token ) {
|
||||||
|
$payment_source = new PaymentSource(
|
||||||
|
'paypal',
|
||||||
|
(object) array(
|
||||||
|
'vault_id' => $token->get_token(),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$this->handle_paypal_order( $wc_order, $order );
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->logger->info(
|
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
|
||||||
sprintf(
|
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), CreditCardGateway::ID );
|
||||||
'Renewal for order %d is completed.',
|
foreach ( $wc_tokens as $token ) {
|
||||||
$wc_order->get_id()
|
$payment_source = new PaymentSource(
|
||||||
)
|
'card',
|
||||||
);
|
(object) array(
|
||||||
|
'vault_id' => $token->get_token(),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $payment_source ) {
|
||||||
|
$order = $this->order_endpoint->create(
|
||||||
|
array( $purchase_unit ),
|
||||||
|
$shipping_preference,
|
||||||
|
$payer,
|
||||||
|
null,
|
||||||
|
'',
|
||||||
|
ApplicationContext::USER_ACTION_CONTINUE,
|
||||||
|
'',
|
||||||
|
array(),
|
||||||
|
$payment_source
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->handle_paypal_order( $wc_order, $order );
|
||||||
|
|
||||||
|
$this->logger->info(
|
||||||
|
sprintf(
|
||||||
|
'Renewal for order %d is completed.',
|
||||||
|
$wc_order->get_id()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -302,18 +341,7 @@ class RenewalHandler {
|
||||||
|
|
||||||
$tokens = $this->repository->all_for_user_id( (int) $customer->get_id() );
|
$tokens = $this->repository->all_for_user_id( (int) $customer->get_id() );
|
||||||
if ( ! $tokens ) {
|
if ( ! $tokens ) {
|
||||||
|
return false;
|
||||||
$error_message = sprintf(
|
|
||||||
'Payment failed. No payment tokens found for customer %d.',
|
|
||||||
$customer->get_id()
|
|
||||||
);
|
|
||||||
|
|
||||||
$wc_order->update_status(
|
|
||||||
'failed',
|
|
||||||
$error_message
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->logger->error( $error_message );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$subscription = function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( $wc_order->get_meta( '_subscription_renewal' ) ) : null;
|
$subscription = function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( $wc_order->get_meta( '_subscription_renewal' ) ) : null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue