You are here

public function PaymentIntentTest::testIntentOrderTotalSync in Commerce Stripe 8

Tests that the order total syncs the payment intent total.

@dataProvider dataProviderOrderSync

Parameters

bool $deleted_gateway: Boolean to determine if the test should delete the gateway.

File

tests/src/Kernel/PaymentIntentTest.php, line 74

Class

PaymentIntentTest
Payment intent tests.

Namespace

Drupal\Tests\commerce_stripe\Kernel

Code

public function testIntentOrderTotalSync($deleted_gateway) {
  $gateway = $this
    ->generateGateway();
  $plugin = $gateway
    ->getPlugin();
  assert($plugin instanceof StripeInterface);
  $order = Order::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'uid' => 0,
    'state' => 'draft',
  ]);
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 1,
    'unit_price' => new Price('10.50', 'USD'),
  ]);
  $order_item
    ->save();
  $order
    ->addItem($order_item);
  $payment_method = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => $gateway
      ->id(),
    'payment_gateway_mode' => 'test',
    'remote_id' => 'pm_card_threeDSecure2Required|',
  ]);
  $payment_method
    ->save();
  $order
    ->set('payment_method', $payment_method);
  $order
    ->set('payment_gateway', $gateway);
  $order
    ->save();
  $intent = $plugin
    ->createPaymentIntent($order);
  $this
    ->assertEquals(1050, $intent->amount);
  if ($deleted_gateway) {
    $gateway
      ->delete();
  }
  $order = $this
    ->reloadEntity($order);
  $order
    ->addAdjustment(new Adjustment([
    'type' => 'custom',
    'label' => 'Back to school discount',
    'amount' => new Price('-5.00', 'USD'),
  ]));
  $order
    ->save();

  // Flush pending updates.
  $this->container
    ->get('commerce_stripe.order_events_subscriber')
    ->destruct();
  $this
    ->assertEquals('5.50', $order
    ->getTotalPrice()
    ->getNumber());
  $intent = PaymentIntent::retrieve($intent->id);

  // If the payment gateway was deleted, the payment intent could not
  // be updated.
  $this
    ->assertEquals($deleted_gateway ? 1050 : 550, $intent->amount);
}