You are here

public function PaymentCheckoutTest::testCheckoutWithDeclinedPaymentMethod in Commerce Core 8.2

Tests that a declined payment does not complete checkout.

File

modules/payment/tests/src/FunctionalJavascript/PaymentCheckoutTest.php, line 446

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testCheckoutWithDeclinedPaymentMethod() {
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('Credit card');
  $radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->defaultAddress, 'payment_information[add_payment_method][billing_information]');
  $this
    ->getSession()
    ->getPage()
    ->fillField('payment_information[add_payment_method][billing_information][select_address]', '_new');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([
    'payment_information[add_payment_method][payment_details][number]' => '4111111111111111',
    'payment_information[add_payment_method][payment_details][expiration][month]' => '02',
    'payment_information[add_payment_method][payment_details][expiration][year]' => '2024',
    'payment_information[add_payment_method][payment_details][security_code]' => '123',
    'payment_information[add_payment_method][billing_information][address][0][address][given_name]' => 'Johnny',
    'payment_information[add_payment_method][billing_information][address][0][address][family_name]' => 'Appleseed',
    'payment_information[add_payment_method][billing_information][address][0][address][address_line1]' => '123 New York Drive',
    'payment_information[add_payment_method][billing_information][address][0][address][locality]' => 'Somewhere',
    'payment_information[add_payment_method][billing_information][address][0][address][administrative_area]' => 'WI',
    'payment_information[add_payment_method][billing_information][address][0][address][postal_code]' => '53140',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->assertSession()
    ->pageTextContains('Visa ending in 1111');
  $this
    ->assertSession()
    ->pageTextContains('Expires 2/2024');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $this
    ->assertSession()
    ->pageTextNotContains('Your order number is 1. You can view your order on your account page when logged in.');
  $this
    ->assertSession()
    ->pageTextContains('We encountered an error processing your payment method. Please verify your details and try again.');
  $this
    ->assertSession()
    ->addressEquals('checkout/1/order_information');
  $order = Order::load(1);
  $this
    ->assertFalse($order
    ->isLocked());

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = $order
    ->get('payment_method')->entity;
  $this
    ->assertEquals('123 New York Drive', $payment_method
    ->getBillingProfile()
    ->get('address')->address_line1);

  // Confirm that the address book profile was not updated.
  $this->defaultProfile = $this
    ->reloadEntity($this->defaultProfile);
  $this
    ->assertEquals('9 Drupal Ave', $this->defaultProfile
    ->get('address')->address_line1);

  // Verify a payment was not created.
  $payment = Payment::load(1);
  $this
    ->assertNull($payment);
}