You are here

public function PaymentCheckoutTest::testCheckoutWithExistingPaymentMethod in Commerce Core 8.2

Tests checkout with an existing payment method.

File

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

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testCheckoutWithExistingPaymentMethod() {
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');

  // Make the order partially paid, to confirm that checkout only charges
  // for the remaining amount.
  $payment = Payment::create([
    'type' => 'payment_default',
    'payment_gateway' => 'onsite',
    'order_id' => '1',
    'amount' => new Price('20', 'USD'),
    'state' => 'completed',
  ]);
  $payment
    ->save();
  $order = Order::load(1);

  // Save the order to recalculate the balance.
  $order
    ->save();
  $this
    ->assertEquals(new Price('20', 'USD'), $order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('19.99', 'USD'), $order
    ->getBalance());
  $this
    ->submitForm([
    'payment_information[payment_method]' => '1',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->assertSession()
    ->pageTextContains('Visa ending in 1111');
  $this
    ->assertSession()
    ->pageTextContains('Expires 3/2028');
  $this
    ->assertSession()
    ->pageTextContains('Frederick Pabst');
  $this
    ->assertSession()
    ->pageTextContains('Pabst Blue Ribbon Dr');
  $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.');
  \Drupal::entityTypeManager()
    ->getStorage('commerce_order')
    ->resetCache([
    1,
  ]);
  $order = Order::load(1);
  $this
    ->assertEquals('onsite', $order
    ->get('payment_gateway')->target_id);
  $this
    ->assertEquals('1', $order
    ->get('payment_method')->target_id);
  $this
    ->assertFalse($order
    ->isLocked());

  // Verify that a completed payment was made.
  $payment = Payment::load(2);
  $this
    ->assertNotNull($payment);
  $this
    ->assertEquals('completed', $payment
    ->getState()
    ->getId());
  $this
    ->assertEquals(new Price('19.99', 'USD'), $payment
    ->getAmount());
  $this
    ->assertEquals(new Price('39.99', 'USD'), $order
    ->getTotalPaid());
  $this
    ->assertEquals(new Price('0', 'USD'), $order
    ->getBalance());
  $this
    ->assertEquals('A', $payment
    ->getAvsResponseCode());
  $this
    ->assertEquals('Address', $payment
    ->getAvsResponseCodeLabel());

  /** @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;

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

  // Verify that the billing information was copied to the order.
  $this
    ->assertTrue($order_billing_profile
    ->equalToProfile($payment_method_profile));
  $this
    ->assertNotEquals($order_billing_profile
    ->id(), $payment_method_profile
    ->id());
}