@@ -380,4 +380,188 @@ public function provide_test_process_webhook_dispute() {
380
380
],
381
381
];
382
382
}
383
+
384
+ /**
385
+ * Test for `process_payment_intent`.
386
+ *
387
+ * @param string $event_type The event type.
388
+ * @param string $order_status The order status.
389
+ * @param bool $order_locked Whether the order is locked.
390
+ * @param string $payment_type The payment method.
391
+ * @param bool $order_status_final Whether the order status is final.
392
+ * @param string $expected_status The expected order status.
393
+ * @param string $expected_note The expected order note.
394
+ * @param int $expected_process_payment_calls The expected number of calls to process_payment.
395
+ * @param int $expected_process_payment_intent_incomplete_calls The expected number of calls to process_payment_intent_incomplete.
396
+ * @return void
397
+ * @dataProvider provide_test_process_payment_intent
398
+ * @throws WC_Data_Exception When order status is invalid.
399
+ */
400
+ public function test_process_payment_intent (
401
+ $ event_type ,
402
+ $ order_status ,
403
+ $ order_locked ,
404
+ $ payment_type ,
405
+ $ order_status_final ,
406
+ $ expected_status ,
407
+ $ expected_note ,
408
+ $ expected_process_payment_calls ,
409
+ $ expected_process_payment_intent_incomplete_calls
410
+ ) {
411
+ $ mock_action_process_payment = new MockAction ();
412
+ add_action (
413
+ 'wc_gateway_stripe_process_payment ' ,
414
+ [ &$ mock_action_process_payment , 'action ' ]
415
+ );
416
+
417
+ $ mock_action_process_payment_intent_incomplete = new MockAction ();
418
+ add_action (
419
+ 'wc_gateway_stripe_process_payment_intent_incomplete ' ,
420
+ [ &$ mock_action_process_payment_intent_incomplete , 'action ' ]
421
+ );
422
+
423
+ $ order = WC_Helper_Order::create_order ();
424
+ $ order ->set_status ( $ order_status );
425
+ if ( $ order_locked ) {
426
+ $ order ->update_meta_data ( '_stripe_lock_payment ' , ( time () + MINUTE_IN_SECONDS ) );
427
+ }
428
+ if ( $ order_status_final ) {
429
+ $ order ->update_meta_data ( '_stripe_status_final ' , true );
430
+ }
431
+ $ order ->update_meta_data ( '_stripe_upe_payment_type ' , $ payment_type );
432
+ $ order ->update_meta_data ( '_stripe_upe_waiting_for_redirect ' , true );
433
+ $ order ->save_meta_data ();
434
+ $ order ->save ();
435
+
436
+ $ notification = (object ) [
437
+ 'type ' => $ event_type ,
438
+ 'data ' => (object ) [
439
+ 'object ' => (object ) [
440
+ 'id ' => 'pi_mock ' ,
441
+ 'metadata ' => (object ) [
442
+ 'order_id ' => $ order ->get_id (),
443
+ ],
444
+ 'last_payment_error ' => (object ) [
445
+ 'message ' => 'Your card was declined. You can call your bank for details. ' ,
446
+ ],
447
+ ],
448
+ ],
449
+ ];
450
+
451
+ $ this ->mock_webhook_handler ->process_payment_intent ( $ notification );
452
+
453
+ $ final_order = wc_get_order ( $ order ->get_id () );
454
+
455
+ $ this ->assertSame ( $ expected_status , $ final_order ->get_status () );
456
+ if ( ! empty ( $ expected_note ) ) {
457
+ $ notes = wc_get_order_notes (
458
+ [
459
+ 'order_id ' => $ final_order ->get_id (),
460
+ 'limit ' => 1 ,
461
+ ]
462
+ );
463
+ $ this ->assertMatchesRegularExpression ( $ expected_note , $ notes [0 ]->content );
464
+ }
465
+
466
+ $ this ->assertEquals ( $ expected_process_payment_calls , $ mock_action_process_payment ->get_call_count () );
467
+ $ this ->assertEquals ( $ expected_process_payment_intent_incomplete_calls , $ mock_action_process_payment_intent_incomplete ->get_call_count () );
468
+ }
469
+
470
+ /**
471
+ * Provider for `test_process_payment_intent`.
472
+ *
473
+ * @return array
474
+ */
475
+ public function provide_test_process_payment_intent () {
476
+ return [
477
+ 'invalid status ' => [
478
+ 'event type ' => 'payment_intent.succeeded ' ,
479
+ 'order status ' => 'cancelled ' ,
480
+ 'order locked ' => false ,
481
+ 'payment type ' => WC_Stripe_Payment_Methods::CARD ,
482
+ 'order status final ' => false ,
483
+ 'expected status ' => 'cancelled ' ,
484
+ 'expected note ' => '' ,
485
+ 'expected process payment calls ' => 0 ,
486
+ 'expected process payment intent incomplete calls ' => 0 ,
487
+ ],
488
+ 'order is locked ' => [
489
+ 'event type ' => 'payment_intent.succeeded ' ,
490
+ 'order status ' => 'pending ' ,
491
+ 'order locked ' => true ,
492
+ 'payment type ' => WC_Stripe_Payment_Methods::CARD ,
493
+ 'order status final ' => false ,
494
+ 'expected status ' => 'pending ' ,
495
+ 'expected note ' => '' ,
496
+ 'expected process payment calls ' => 0 ,
497
+ 'expected process payment intent incomplete calls ' => 0 ,
498
+ ],
499
+ 'success, payment_intent.requires_action, voucher payment ' => [
500
+ 'event type ' => 'payment_intent.requires_action ' ,
501
+ 'order status ' => 'pending ' ,
502
+ 'order locked ' => false ,
503
+ 'payment type ' => WC_Stripe_Payment_Methods::BOLETO ,
504
+ 'order status final ' => false ,
505
+ 'expected status ' => 'on-hold ' ,
506
+ 'expected note ' => '/Awaiting payment. Order status changed from Pending payment to On hold./ ' ,
507
+ 'expected process payment calls ' => 0 ,
508
+ 'expected process payment intent incomplete calls ' => 0 ,
509
+ ],
510
+ 'success, payment_intent.succeeded, voucher payment ' => [
511
+ 'event type ' => 'payment_intent.succeeded ' ,
512
+ 'order status ' => 'pending ' ,
513
+ 'order locked ' => false ,
514
+ 'payment type ' => WC_Stripe_Payment_Methods::BOLETO ,
515
+ 'order status final ' => false ,
516
+ 'expected status ' => 'pending ' ,
517
+ 'expected note ' => '' ,
518
+ 'expected process payment calls ' => 1 ,
519
+ 'expected process payment intent incomplete calls ' => 0 ,
520
+ ],
521
+ 'success, payment_intent.amount_capturable_updated, async payment, awaiting action ' => [
522
+ 'event type ' => 'payment_intent.amount_capturable_updated ' ,
523
+ 'order status ' => 'pending ' ,
524
+ 'order locked ' => false ,
525
+ 'payment type ' => WC_Stripe_Payment_Methods::CARD ,
526
+ 'order status final ' => false ,
527
+ 'expected status ' => 'pending ' ,
528
+ 'expected note ' => '' ,
529
+ 'expected process payment calls ' => 0 ,
530
+ 'expected process payment intent incomplete calls ' => 1 ,
531
+ ],
532
+ 'success, payment_intent.payment_failed, voucher payment ' => [
533
+ 'event type ' => 'payment_intent.payment_failed ' ,
534
+ 'order status ' => 'pending ' ,
535
+ 'order locked ' => false ,
536
+ 'payment type ' => WC_Stripe_Payment_Methods::BOLETO ,
537
+ 'order status final ' => false ,
538
+ 'expected status ' => 'failed ' ,
539
+ 'expected note ' => '/Payment not completed in time Order status changed from Pending payment to Failed./ ' ,
540
+ 'expected process payment calls ' => 0 ,
541
+ 'expected process payment intent incomplete calls ' => 0 ,
542
+ ],
543
+ 'success, payment_intent.payment_failed, IPP ' => [
544
+ 'event type ' => 'payment_intent.payment_failed ' ,
545
+ 'order status ' => 'pending ' ,
546
+ 'order locked ' => false ,
547
+ 'payment type ' => WC_Stripe_Payment_Methods::CARD_PRESENT ,
548
+ 'order status final ' => false ,
549
+ 'expected status ' => 'failed ' ,
550
+ 'expected note ' => '/Stripe SCA authentication failed. Reason: Your card was declined. You can call your bank for details. Order status changed from Pending payment to Failed./ ' ,
551
+ 'expected process payment calls ' => 0 ,
552
+ 'expected process payment intent incomplete calls ' => 0 ,
553
+ ],
554
+ 'success, payment_intent.payment_failed, IPP, status final ' => [
555
+ 'event type ' => 'payment_intent.payment_failed ' ,
556
+ 'order status ' => 'pending ' ,
557
+ 'order locked ' => false ,
558
+ 'payment type ' => WC_Stripe_Payment_Methods::CARD_PRESENT ,
559
+ 'order status final ' => true ,
560
+ 'expected status ' => 'pending ' ,
561
+ 'expected note ' => '/Stripe SCA authentication failed. Reason: Your card was declined. You can call your bank for details./ ' ,
562
+ 'expected process payment calls ' => 0 ,
563
+ 'expected process payment intent incomplete calls ' => 0 ,
564
+ ],
565
+ ];
566
+ }
383
567
}
0 commit comments