Skip to content

Commit

Permalink
Move vault v3 renewal logic from filter to renewal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Emili Castells Guasch authored and Emili Castells Guasch committed Dec 20, 2023
1 parent 6b843bc commit 4cc03cc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
25 changes: 4 additions & 21 deletions modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,6 @@ function( array $localized_script_data ) use ( $c ) {
add_filter(
'ppcp_create_order_request_body_data',
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 ) {
$save_payment_method = $request_data['save_payment_method'] ?? false;
if ( $save_payment_method ) {
Expand All @@ -114,6 +93,10 @@ function( array $data, string $payment_method, array $request_data ): array {
);

$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 ) {
$data['payment_source']['card']['attributes']['customer'] = array(
'id' => $target_customer_id,
Expand Down
76 changes: 52 additions & 24 deletions modules/ppcp-wc-subscriptions/src/RenewalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace WooCommerce\PayPalCommerce\WcSubscriptions;

use WC_Subscription;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
Expand All @@ -24,6 +25,7 @@
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
Expand Down Expand Up @@ -194,6 +196,7 @@ private function process_order( \WC_Order $wc_order ): void {
'renewal'
);

// Vault v2.
$token = $this->get_token_for_customer( $customer, $wc_order );
if ( $token ) {
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
Expand Down Expand Up @@ -267,20 +270,56 @@ private function process_order( \WC_Order $wc_order ): void {
return;
}

$order = $this->order_endpoint->create(
array( $purchase_unit ),
$shipping_preference,
$payer
);
// Vault v3.
$payment_source = null;
if ( $wc_order->get_payment_method() === PayPalGateway::ID ) {
$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(
sprintf(
'Renewal for order %d is completed.',
$wc_order->get_id()
)
);
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), CreditCardGateway::ID );
foreach ( $wc_tokens as $token ) {
$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()
)
);
}
}

/**
Expand All @@ -302,18 +341,7 @@ private function get_token_for_customer( \WC_Customer $customer, \WC_Order $wc_o

$tokens = $this->repository->all_for_user_id( (int) $customer->get_id() );
if ( ! $tokens ) {

$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 );
return false;
}

$subscription = function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( $wc_order->get_meta( '_subscription_renewal' ) ) : null;
Expand Down

0 comments on commit 4cc03cc

Please sign in to comment.