You are here

public function PaymentCheckoutTest::testCheckoutWithNewPaymentMethod in Commerce Core 8.2

Tests checkout with a new payment method.

File

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

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testCheckoutWithNewPaymentMethod() {

  // Note that we test with a different user with less rights to ensure
  // the billing profile can be viewed on the review step.
  $this
    ->drupalLogin($this->user);
  $this->defaultProfile
    ->setOwner($this->user);
  $this->defaultProfile
    ->save();

  // Test the 'capture' setting of PaymentProcess while here.

  /** @var \Drupal\commerce_checkout\Entity\CheckoutFlow $checkout_flow */
  $checkout_flow = CheckoutFlow::load('default');
  $plugin = $checkout_flow
    ->getPlugin();
  $configuration = $plugin
    ->getConfiguration();
  $configuration['panes']['payment_process']['capture'] = FALSE;
  $plugin
    ->setConfiguration($configuration);
  $checkout_flow
    ->save();
  $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()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([
    'payment_information[add_payment_method][payment_details][number]' => '4012888888881881',
    '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]' => 'New York City',
    'payment_information[add_payment_method][billing_information][address][0][address][administrative_area]' => 'NY',
    'payment_information[add_payment_method][billing_information][address][0][address][postal_code]' => '10001',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->assertSession()
    ->pageTextContains('Visa ending in 1881');
  $this
    ->assertSession()
    ->pageTextContains('Expires 2/2024');
  $this
    ->assertSession()
    ->pageTextContains('Johnny Appleseed');
  $this
    ->assertSession()
    ->pageTextContains('123 New York Drive');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
  $order = Order::load(1);
  $this
    ->assertFalse($order
    ->isLocked());
  $this
    ->assertEquals('onsite', $order
    ->get('payment_gateway')->target_id);

  /** @var \Drupal\profile\Entity\ProfileInterface $order_billing_profile */
  $order_billing_profile = $order
    ->getBillingProfile();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = $order
    ->get('payment_method')->entity;
  $this
    ->assertEquals('1881', $payment_method
    ->get('card_number')->value);

  // Confirm that the billing profile has the expected address.
  $expected_address = [
    'given_name' => 'Johnny',
    'family_name' => 'Appleseed',
    'address_line1' => '123 New York Drive',
    'locality' => 'New York City',
    'administrative_area' => 'NY',
    'postal_code' => '10001',
    'country_code' => 'US',
  ];
  $payment_method_profile = $payment_method
    ->getBillingProfile();
  $this
    ->assertEquals($expected_address, array_filter($payment_method_profile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertNotEmpty($payment_method_profile
    ->getData('address_book_profile_id'));
  $this
    ->assertEmpty($payment_method_profile
    ->getData('copy_to_address_book'));

  // Verify that the billing information was copied to the order.
  $this
    ->assertEquals($expected_address, array_filter($order_billing_profile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertNotEquals($order_billing_profile
    ->id(), $payment_method_profile
    ->id());
  $this
    ->assertNotEmpty($order_billing_profile
    ->getData('address_book_profile_id'));
  $this
    ->assertEmpty($order_billing_profile
    ->getData('copy_to_address_book'));

  // Confirm that the address book profile was updated.
  $this->defaultProfile = $this
    ->reloadEntity($this->defaultProfile);
  $this
    ->assertEquals($expected_address, array_filter($this->defaultProfile
    ->get('address')
    ->first()
    ->toArray()));

  // Verify that a payment was created.
  $payment = Payment::load(1);
  $this
    ->assertNotNull($payment);
  $this
    ->assertEquals($payment
    ->getAmount(), $order
    ->getTotalPrice());
  $this
    ->assertEquals('authorization', $payment
    ->getState()
    ->getId());
  $this
    ->assertEquals('A', $payment
    ->getAvsResponseCode());
  $this
    ->assertEquals('Address', $payment
    ->getAvsResponseCodeLabel());
}