You are here

public function PaymentCheckoutTest::testCheckoutWithOffsitePaymentNotify in Commerce Core 8.2

Tests checkout with an off-site gateway that supports notifications.

We simulate onNotify() being called before onReturn(), resulting in the order being fully paid and placed before the customer returns to the site.

File

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

Class

PaymentCheckoutTest
Tests the integration between payments and checkout.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testCheckoutWithOffsitePaymentNotify() {
  $payment_gateway = PaymentGateway::load('offsite');
  $payment_gateway
    ->setPluginConfiguration([
    'redirect_method' => 'post_manual',
    'payment_method_types' => [
      'credit_card',
    ],
  ]);
  $payment_gateway
    ->save();
  $this
    ->drupalGet($this->product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('Example');
  $radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->defaultAddress, 'payment_information[billing_information]');
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->assertSession()
    ->pageTextContains('Example');
  $this
    ->assertSession()
    ->pageTextContains('Bryan Centarro');
  $this
    ->assertSession()
    ->pageTextContains('9 Drupal Ave');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $this
    ->assertSession()
    ->addressEquals('checkout/1/payment');

  // Simulate the order being paid in full.
  $payment = Payment::create([
    'type' => 'payment_default',
    'payment_gateway' => 'offsite',
    'order_id' => '1',
    'amount' => new Price('39.99', 'USD'),
    'state' => 'completed',
  ]);
  $payment
    ->save();
  $order = Order::load(1);

  // Save the order to recalculate the balance.
  $order
    ->save();
  $this
    ->assertTrue($order
    ->isPaid());
  $this
    ->assertFalse($order
    ->isLocked());

  // Go to the return url and confirm that it works.
  $this
    ->drupalGet('checkout/1/payment/return');
  $this
    ->assertSession()
    ->addressEquals('checkout/1/complete');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');

  /** @var \Drupal\commerce_payment\PaymentStorageInterface $payment_storage */
  $payment_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_payment');

  // Confirm that only one payment was made.
  $payments = $payment_storage
    ->loadMultipleByOrder($order);
  $this
    ->assertCount(1, $payments);
}