Skip to content

Commit 4cc03cc

Browse files
Emili Castells GuaschEmili Castells Guasch
authored andcommitted
Move vault v3 renewal logic from filter to renewal handler
1 parent 6b843bc commit 4cc03cc

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,6 @@ function( array $localized_script_data ) use ( $c ) {
7979
add_filter(
8080
'ppcp_create_order_request_body_data',
8181
function( array $data, string $payment_method, array $request_data ): array {
82-
// phpcs:ignore WordPress.Security.NonceVerification.Missing
83-
$wc_order_action = wc_clean( wp_unslash( $_POST['wc_order_action'] ?? '' ) );
84-
if ( $wc_order_action === 'wcs_process_renewal' ) {
85-
// phpcs:ignore WordPress.Security.NonceVerification.Missing
86-
$subscription_id = wc_clean( wp_unslash( $_POST['post_ID'] ?? '' ) );
87-
$subscription = wcs_get_subscription( (int) $subscription_id );
88-
if ( $subscription ) {
89-
$customer_id = $subscription->get_customer_id();
90-
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
91-
foreach ( $wc_tokens as $token ) {
92-
$data['payment_source'] = array(
93-
'paypal' => array(
94-
'vault_id' => $token->get_token(),
95-
),
96-
);
97-
98-
return $data;
99-
}
100-
}
101-
}
102-
10382
if ( $payment_method === CreditCardGateway::ID ) {
10483
$save_payment_method = $request_data['save_payment_method'] ?? false;
10584
if ( $save_payment_method ) {
@@ -114,6 +93,10 @@ function( array $data, string $payment_method, array $request_data ): array {
11493
);
11594

11695
$target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
96+
if ( ! $target_customer_id ) {
97+
$target_customer_id = get_user_meta( get_current_user_id(), 'ppcp_customer_id', true );
98+
}
99+
117100
if ( $target_customer_id ) {
118101
$data['payment_source']['card']['attributes']['customer'] = array(
119102
'id' => $target_customer_id,

modules/ppcp-wc-subscriptions/src/RenewalHandler.php

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WooCommerce\PayPalCommerce\WcSubscriptions;
1111

1212
use WC_Subscription;
13+
use WC_Payment_Tokens;
1314
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
1415
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
1516
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
@@ -24,6 +25,7 @@
2425
use Psr\Log\LoggerInterface;
2526
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
2627
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
28+
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
2729
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
2830
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
2931
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
@@ -194,6 +196,7 @@ private function process_order( \WC_Order $wc_order ): void {
194196
'renewal'
195197
);
196198

199+
// Vault v2.
197200
$token = $this->get_token_for_customer( $customer, $wc_order );
198201
if ( $token ) {
199202
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
@@ -267,20 +270,56 @@ private function process_order( \WC_Order $wc_order ): void {
267270
return;
268271
}
269272

270-
$order = $this->order_endpoint->create(
271-
array( $purchase_unit ),
272-
$shipping_preference,
273-
$payer
274-
);
273+
// Vault v3.
274+
$payment_source = null;
275+
if ( $wc_order->get_payment_method() === PayPalGateway::ID ) {
276+
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), PayPalGateway::ID );
277+
foreach ( $wc_tokens as $token ) {
278+
$payment_source = new PaymentSource(
279+
'paypal',
280+
(object) array(
281+
'vault_id' => $token->get_token(),
282+
)
283+
);
275284

276-
$this->handle_paypal_order( $wc_order, $order );
285+
break;
286+
}
287+
}
277288

278-
$this->logger->info(
279-
sprintf(
280-
'Renewal for order %d is completed.',
281-
$wc_order->get_id()
282-
)
283-
);
289+
if ( $wc_order->get_payment_method() === CreditCardGateway::ID ) {
290+
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), CreditCardGateway::ID );
291+
foreach ( $wc_tokens as $token ) {
292+
$payment_source = new PaymentSource(
293+
'card',
294+
(object) array(
295+
'vault_id' => $token->get_token(),
296+
)
297+
);
298+
}
299+
}
300+
301+
if ( $payment_source ) {
302+
$order = $this->order_endpoint->create(
303+
array( $purchase_unit ),
304+
$shipping_preference,
305+
$payer,
306+
null,
307+
'',
308+
ApplicationContext::USER_ACTION_CONTINUE,
309+
'',
310+
array(),
311+
$payment_source
312+
);
313+
314+
$this->handle_paypal_order( $wc_order, $order );
315+
316+
$this->logger->info(
317+
sprintf(
318+
'Renewal for order %d is completed.',
319+
$wc_order->get_id()
320+
)
321+
);
322+
}
284323
}
285324

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

303342
$tokens = $this->repository->all_for_user_id( (int) $customer->get_id() );
304343
if ( ! $tokens ) {
305-
306-
$error_message = sprintf(
307-
'Payment failed. No payment tokens found for customer %d.',
308-
$customer->get_id()
309-
);
310-
311-
$wc_order->update_status(
312-
'failed',
313-
$error_message
314-
);
315-
316-
$this->logger->error( $error_message );
344+
return false;
317345
}
318346

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

0 commit comments

Comments
 (0)