You are here

protected function BraintreeApiIntegrationTest::generateTestPayment in Commerce Braintree 8

Generates a test payment to send over the Braintree gateway.

Parameters

bool $billing_profile: Whether a billing profile should be added.

string $amount: The test amount.

string $nonce: The test nonce.

Return value

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

See also

https://developers.braintreepayments.com/reference/general/testing/php#t...

5 calls to BraintreeApiIntegrationTest::generateTestPayment()
BraintreeApiIntegrationTest::testCreatePayment in tests/src/Kernel/BraintreeApiIntegrationTest.php
Tests creating a payment.
BraintreeApiIntegrationTest::testCreatePaymentApplicationIncomplete in tests/src/Kernel/BraintreeApiIntegrationTest.php
Tests creating a payment, with application incomplete.
BraintreeApiIntegrationTest::testCreatePaymentInsufficentFunds in tests/src/Kernel/BraintreeApiIntegrationTest.php
Tests creating a payment, with insufficient funds.
BraintreeApiIntegrationTest::testCreatePaymentProcessorDeclined in tests/src/Kernel/BraintreeApiIntegrationTest.php
Tests creating a payment, with processor declined.
BraintreeApiIntegrationTest::testCreatePaymentRejectedConsumedNonce in tests/src/Kernel/BraintreeApiIntegrationTest.php
Tests a re-used nonce.

File

tests/src/Kernel/BraintreeApiIntegrationTest.php, line 154

Class

BraintreeApiIntegrationTest
Tests the Braintree SDK integration.

Namespace

Drupal\Tests\commerce_braintree\Kernel

Code

protected function generateTestPayment($billing_profile, $amount, $nonce = NULL) {
  if ($nonce === NULL) {
    $nonce = \Braintree\Test\Nonces::$transactable;
  }
  $user = $this
    ->createUser();
  $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',
  ]);
  $order
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => $this->gateway
      ->id(),
    'uid' => $user
      ->id(),
    'remote_id' => $nonce,
  ]);
  $payment_method
    ->setReusable(FALSE);
  if ($billing_profile) {

    /** @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();
    $payment_method
      ->setBillingProfile($profile);
  }
  $payment_method
    ->save();
  $payment = Payment::create([
    'state' => 'new',
    'amount' => new Price($amount, 'USD'),
    'payment_gateway' => $this->gateway
      ->id(),
    'order_id' => $order
      ->id(),
  ]);
  $payment->payment_method = $payment_method;
  return $payment;
}