You are here

public function PayflowLink::createSecureToken in Commerce PayPal 8

Requests a secure token from Payflow for use in follow-up API requests.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order whose details should be submitted in the secure token request.

Return value

string|null The secure Token if successfully retrieved, NULL on failure.

Throws

\Drupal\Core\TypedData\Exception\MissingDataException

Overrides PayflowLinkInterface::createSecureToken

File

src/Plugin/Commerce/PaymentGateway/PayflowLink.php, line 516

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function createSecureToken(OrderInterface $order) {
  $cancel_url = Url::fromRoute('commerce_payment.checkout.cancel', [
    'commerce_order' => $order
      ->id(),
    'step' => 'payment',
  ], [
    'absolute' => TRUE,
  ])
    ->toString();
  $return_url = Url::fromRoute('commerce_payment.checkout.return', [
    'commerce_order' => $order
      ->id(),
    'step' => 'payment',
  ], [
    'absolute' => TRUE,
  ])
    ->toString();

  // Build a name-value pair array for this transaction.
  $nvp = [
    // Request a secure token using our order's token ID.
    'CREATESECURETOKEN' => 'Y',
    'SECURETOKENID' => $order
      ->getData('commerce_payflow')['tokenid'],
    // Indicate the type and amount of the transaction.
    'TRXTYPE' => $this->configuration['trxtype'],
    'AMT' => Calculator::trim($order
      ->getTotalPrice()
      ->getNumber()),
    'CURRENCY' => $order
      ->getTotalPrice()
      ->getCurrencyCode(),
    'INVNUM' => $order
      ->id() . '-' . $this->time
      ->getRequestTime(),
    // Add application specific parameters.
    'BUTTONSOURCE' => self::BUTTON_SOURCE,
    'ERRORURL' => $return_url,
    'RETURNURL' => $return_url,
    'CANCELURL' => $cancel_url,
    'DISABLERECEIPT' => 'TRUE',
    'TEMPLATE' => 'TEMPLATEA',
    'CSCREQUIRED' => 'TRUE',
    'CSCEDIT' => 'TRUE',
    'URLMETHOD' => 'POST',
  ];

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

  // Prepare the billing address for use in the request.
  if ($billing_profile && !$billing_profile
    ->get('address')
    ->isEmpty()) {
    $billing_address = $billing_profile
      ->get('address')
      ->first()
      ->getValue();
    if (is_array($billing_address)) {

      // Add the billing address.
      $nvp += [
        'BILLTOEMAIL' => mb_substr($order
          ->getEmail(), 0, 60),
        'BILLTOFIRSTNAME' => mb_substr($billing_address['given_name'], 0, 45),
        'BILLTOLASTNAME' => mb_substr($billing_address['family_name'], 0, 45),
        'BILLTOSTREET' => mb_substr($billing_address['address_line1'], 0, 150),
        'BILLTOCITY' => mb_substr($billing_address['locality'], 0, 45),
        'BILLTOSTATE' => mb_substr($billing_address['administrative_area'], 0, 2),
        'BILLTOCOUNTRY' => mb_substr($billing_address['country_code'], 0, 2),
        'BILLTOZIP' => mb_substr($billing_address['postal_code'], 0, 10),
      ];
    }
  }

  // If enabled, email the customer a receipt from PayPal.
  if (!empty($this->configuration['emailcustomer'])) {
    $nvp['EMAILCUSTOMER'] = 'TRUE';
  }

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

  // Prepare the billing address for use in the request.
  if ($shipping_profile && !$shipping_profile
    ->get('address')
    ->isEmpty()) {
    $shipping_address = $shipping_profile
      ->get('address')
      ->first()
      ->getValue();
    if (is_array($shipping_address)) {

      // Add the shipping address parameters to the request.
      $nvp += [
        'SHIPTOFIRSTNAME' => mb_substr($shipping_address['given_name'], 0, 45),
        'SHIPTOLASTNAME' => mb_substr($shipping_address['family_name'], 0, 45),
        'SHIPTOSTREET' => mb_substr($shipping_address['address_line1'], 0, 150),
        'SHIPTOCITY' => mb_substr($shipping_address['locality'], 0, 45),
        'SHIPTOSTATE' => mb_substr($shipping_address['administrative_area'], 0, 2),
        'SHIPTOCOUNTRY' => mb_substr($shipping_address['country_code'], 0, 2),
        'SHIPTOZIP' => mb_substr($shipping_address['postal_code'], 0, 10),
      ];
    }
  }

  // Add the line item details to the array.
  $nvp += $this
    ->itemizeOrder($order);

  // Submit the API request to Payflow.
  $response = $this
    ->apiRequest('pro', $nvp, $order);

  // If the request is successful, return the token.
  if (isset($response['RESULT']) && $response['RESULT'] == '0') {
    return $response['SECURETOKEN'];
  }

  // Otherwise indicate failure by returning NULL.
  return NULL;
}