You are here

public function CheckoutTest::testCheckoutWithExistingPaymentMethodOffSession in Commerce Stripe 8

Tests checkout with a previously created payment method.

@dataProvider dataProviderExistingPaymentMethodCardNumber @group threeds @group existing @group off_session

File

tests/src/FunctionalJavascript/CheckoutTest.php, line 329

Class

CheckoutTest
Tests checkout with Stripe.

Namespace

Drupal\Tests\commerce_stripe\FunctionalJavascript

Code

public function testCheckoutWithExistingPaymentMethodOffSession($card_number) {
  $customer = $this
    ->createUser([
    'manage own commerce_payment_method',
  ]);
  $this
    ->drupalLogin($customer);
  $this
    ->drupalGet(Url::fromRoute('entity.commerce_payment_method.add_form', [
    'user' => $customer
      ->id(),
  ]));
  $this
    ->fillCreditCardData($card_number, '0322', '123');
  $this
    ->submitForm([
    'add_payment_method[billing_information][address][0][address][given_name]' => 'Johnny',
    'add_payment_method[billing_information][address][0][address][family_name]' => 'Appleseed',
    'add_payment_method[billing_information][address][0][address][address_line1]' => '123 New York Drive',
    'add_payment_method[billing_information][address][0][address][locality]' => 'New York City',
    'add_payment_method[billing_information][address][0][address][administrative_area]' => 'NY',
    'add_payment_method[billing_information][address][0][address][postal_code]' => '10001',
  ], 'Save');
  $this
    ->complete3ds(TRUE, FALSE);
  $this
    ->assertWaitForText('Visa ending in ' . substr($card_number, -4) . ' saved to your payment methods.');
  $this
    ->drupalGet(Url::fromRoute('entity.commerce_payment_method.collection', [
    'user' => $customer
      ->id(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains('Visa ending in ' . substr($card_number, -4));

  // Create an off_session order with the payment method generated.
  $cart_provider = $this->container
    ->get('commerce_cart.cart_provider');
  $cart_manager = $this->container
    ->get('commerce_cart.cart_manager');
  $cart = $cart_provider
    ->createCart('default', $this->store, $customer);
  $cart_manager
    ->addEntity($cart, $this->product
    ->getDefaultVariation());
  $gateway = PaymentGateway::load('stripe_testing');
  $payment_method = PaymentMethod::load(1);
  $cart
    ->set('billing_profile', $payment_method
    ->getBillingProfile());
  $cart
    ->set('payment_method', $payment_method);
  $cart
    ->set('payment_gateway', $gateway
    ->id());
  $cart
    ->save();
  $plugin = $gateway
    ->getPlugin();
  assert($plugin instanceof StripeInterface);
  $plugin
    ->createPaymentIntent($cart);
  $payment = Payment::create([
    'state' => 'new',
    'amount' => $cart
      ->getBalance(),
    'payment_gateway' => $gateway,
    'payment_method' => $payment_method,
    'order_id' => $cart,
  ]);

  // @todo 4000003800000446 _should_ not require authentication. Supposedly.
  // Discussed with Stripe support in IRC and they could not confirm.
  $this
    ->expectException(SoftDeclineException::class);
  $this
    ->expectExceptionMessage('The payment intent requires action by the customer for authentication');
  try {
    $plugin
      ->createPayment($payment);
  } catch (HardDeclineException $e) {
    $this
      ->fail($e
      ->getMessage());
  }
}