10
10
namespace WooCommerce \PayPalCommerce \WcSubscriptions ;
11
11
12
12
use WC_Subscription ;
13
+ use WC_Payment_Tokens ;
13
14
use WooCommerce \PayPalCommerce \ApiClient \Endpoint \OrderEndpoint ;
14
15
use WooCommerce \PayPalCommerce \ApiClient \Entity \ApplicationContext ;
15
16
use WooCommerce \PayPalCommerce \ApiClient \Entity \Order ;
24
25
use Psr \Log \LoggerInterface ;
25
26
use WooCommerce \PayPalCommerce \WcGateway \Exception \NotFoundException ;
26
27
use WooCommerce \PayPalCommerce \WcGateway \Gateway \CreditCardGateway ;
28
+ use WooCommerce \PayPalCommerce \WcGateway \Gateway \PayPalGateway ;
27
29
use WooCommerce \PayPalCommerce \WcGateway \Processor \AuthorizedPaymentsProcessor ;
28
30
use WooCommerce \PayPalCommerce \WcGateway \Processor \OrderMetaTrait ;
29
31
use WooCommerce \PayPalCommerce \WcGateway \Processor \PaymentsStatusHandlingTrait ;
@@ -194,6 +196,7 @@ private function process_order( \WC_Order $wc_order ): void {
194
196
'renewal '
195
197
);
196
198
199
+ // Vault v2.
197
200
$ token = $ this ->get_token_for_customer ( $ customer , $ wc_order );
198
201
if ( $ token ) {
199
202
if ( $ wc_order ->get_payment_method () === CreditCardGateway::ID ) {
@@ -267,20 +270,56 @@ private function process_order( \WC_Order $wc_order ): void {
267
270
return ;
268
271
}
269
272
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
+ );
275
284
276
- $ this ->handle_paypal_order ( $ wc_order , $ order );
285
+ break ;
286
+ }
287
+ }
277
288
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
+ }
284
323
}
285
324
286
325
/**
@@ -302,18 +341,7 @@ private function get_token_for_customer( \WC_Customer $customer, \WC_Order $wc_o
302
341
303
342
$ tokens = $ this ->repository ->all_for_user_id ( (int ) $ customer ->get_id () );
304
343
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 ;
317
345
}
318
346
319
347
$ subscription = function_exists ( 'wcs_get_subscription ' ) ? wcs_get_subscription ( $ wc_order ->get_meta ( '_subscription_renewal ' ) ) : null ;
0 commit comments