You are here

protected function SquareApiIntegrationTest::generateTestPayment in Commerce Square Connect 8

Generates a test payment to send over the Square gateway.

Square provides specific nonce values which can test different error codes, and how to handle them.

Parameters

string $nonce: The test nonce.

Return value

\Drupal\commerce_payment\Entity\PaymentInterface The test payment.

See also

https://docs.connect.squareup.com/articles/using-sandbox

6 calls to SquareApiIntegrationTest::generateTestPayment()
SquareApiIntegrationTest::testCreatePayment in tests/src/Kernel/SquareApiIntegrationTest.php
Tests creating a payment.
SquareApiIntegrationTest::testCreatePaymentAlreadyUsed in tests/src/Kernel/SquareApiIntegrationTest.php
Tests creating a payment, invalid/used nonce.
SquareApiIntegrationTest::testCreatePaymentBadCvv in tests/src/Kernel/SquareApiIntegrationTest.php
Tests creating a payment, with invalid CVV error.
SquareApiIntegrationTest::testCreatePaymentBadExpiryDate in tests/src/Kernel/SquareApiIntegrationTest.php
Tests creating a payment, with invalid expiration date error.
SquareApiIntegrationTest::testCreatePaymentBadPostalCode in tests/src/Kernel/SquareApiIntegrationTest.php
Tests creating a payment, with invalid postal code error.

... See full list

File

tests/src/Kernel/SquareApiIntegrationTest.php, line 180

Class

SquareApiIntegrationTest
Tests the Square SDK integration with Commerce.

Namespace

Drupal\Tests\commerce_square\Kernel

Code

protected function generateTestPayment($nonce) {
  $user = $this
    ->createUser();

  /** @var \Drupal\commerce_order\Entity\Order $order */
  $order = Order::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'state' => 'draft',
    'mail' => 'text@example.com',
    'uid' => $user
      ->id(),
    'ip_address' => '127.0.0.1',
  ]);

  /** @var \Drupal\commerce_order\Entity\OrderItem $order_item */
  $order_item = OrderItem::create([
    'type' => 'test',
    'title' => 'My order item',
    'quantity' => '2',
    'unit_price' => new Price('5.00', 'USD'),
  ]);
  $order_item
    ->save();
  $order
    ->addItem($order_item);
  $order
    ->save();

  /** @var \Drupal\profile\Entity\ProfileInterface $profile */
  $profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'US',
      'postal_code' => '53177',
      'locality' => 'Milwaukee',
      'address_line1' => 'Pabst Blue Ribbon Dr',
      'administrative_area' => 'WI',
      'given_name' => 'Frederick',
      'family_name' => 'Pabst',
    ],
    'uid' => $user
      ->id(),
  ]);
  $profile
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => $this->gateway
      ->id(),
    // 01/10/2022
    'expires' => '1641772800',
    'uid' => $user
      ->id(),
    'remote_id' => $nonce,
  ]);
  $payment_method
    ->setBillingProfile($profile);
  $payment_method
    ->save();
  $payment = Payment::create([
    'state' => 'new',
    'amount' => new Price('10.00', 'USD'),
    'payment_gateway' => $this->gateway
      ->id(),
    'payment_method' => $payment_method
      ->id(),
    'order_id' => $order
      ->id(),
  ]);
  return $payment;
}