You are here

function commerce_payflow_link_create_secure_token in Commerce PayPal 7.2

Requests a Secure Token from Payflow for use in follow-up API requests.

Parameters

$payment_method: The payment method instance to use to generate the Secure Token.

$order: The order whose details should be submitted in the Secure Token request.

$billing_agreement: The name to use for PayPal billing agreements if the customer pays via Express Checkout and the site needs to perform reference transactions.

Return value

The Secure Token if successfully retrieved or FALSE on failure.

1 call to commerce_payflow_link_create_secure_token()
commerce_payflow_link_submit_form_submit in modules/payflow/commerce_payflow.module
Payment method callback: submit form submission.

File

modules/payflow/commerce_payflow.module, line 1017
Implements PayPal Payments Advanced (U.S. only) and Payflow Link Hosted Checkout pages and Transparent Redirect.

Code

function commerce_payflow_link_create_secure_token($payment_method, $order, $billing_agreement = NULL) {

  // Extract the order total value array.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_total = $order_wrapper->commerce_order_total
    ->value();

  // Determine the currency code to use to actually process the transaction,
  // which will either be the default currency code or the currency code of the
  // order if it's supported by PayPal if that option is enabled.
  $currency_code = $payment_method['settings']['currency_code'];
  $order_currency_code = $order_total['currency_code'];
  if (!empty($settings['allow_supported_currencies']) && in_array($order_currency_code, array_keys(commerce_paypal_currencies($payment_method['method_id'])))) {
    $currency_code = $order_currency_code;
  }

  // Prepare a transaction amount value in the proper currency.
  $amt = commerce_currency_convert($order_total['amount'], $order_total['currency_code'], $currency_code);

  // Prepare the billing address for use in the request.
  $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address
    ->value();
  if (empty($billing_address['first_name'])) {
    $name_parts = explode(' ', $billing_address['name_line']);
    $billing_address['first_name'] = array_shift($name_parts);
    $billing_address['last_name'] = implode(' ', $name_parts);
  }

  // Build a name-value pair array for this transaction.
  $nvp = array(
    // Request a secure token using our order's token ID.
    'CREATESECURETOKEN' => 'Y',
    'SECURETOKENID' => $order->data['commerce_payflow']['tokenid'],
    // Indicate the type and amount of the transaction.
    'TRXTYPE' => $payment_method['settings']['trxtype'],
    'AMT' => commerce_paypal_price_amount($amt, $currency_code),
    'CURRENCY' => $currency_code,
    'INVNUM' => commerce_paypal_ipn_invoice($order),
    // Add the billing address.
    'BILLTOEMAIL' => substr($order->mail, 0, 60),
    'BILLTOFIRSTNAME' => substr($billing_address['first_name'], 0, 45),
    'BILLTOLASTNAME' => substr($billing_address['last_name'], 0, 45),
    'BILLTOSTREET' => substr($billing_address['thoroughfare'], 0, 150),
    'BILLTOCITY' => substr($billing_address['locality'], 0, 45),
    'BILLTOSTATE' => substr($billing_address['administrative_area'], 0, 2),
    'BILLTOCOUNTRY' => substr($billing_address['country'], 0, 2),
    'BILLTOZIP' => substr($billing_address['postal_code'], 0, 10),
    // Add application specific parameters.
    'BUTTONSOURCE' => $payment_method['buttonsource'],
    'ERRORURL' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
      'absolute' => TRUE,
    )),
    'RETURNURL' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
      'absolute' => TRUE,
    )),
    'CANCELURL' => url('checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array(
      'absolute' => TRUE,
    )),
    'DISABLERECEIPT' => 'TRUE',
    // @todo Support the 'MOBILE' template if we have a way to determine the
    // site is currently using a responsive theme.
    'TEMPLATE' => $payment_method['settings']['redirect_mode'] == 'iframe' ? 'MINLAYOUT' : 'TEMPLATEA',
    'CSCREQUIRED' => 'TRUE',
    'CSCEDIT' => 'TRUE',
    'URLMETHOD' => 'POST',
    // Store the payment redirect key in a custom field.
    'USER1' => $order->data['payment_redirect_key'],
  );

  // If Express Checkout is enabled, include the IPN URL.
  if (module_exists('commerce_paypal_ec') && !empty($payment_method['settings']['paypal_ec_instance'])) {

    // Ensure the Rule is still valid to receive Express Checkout IPNs.
    if (in_array($payment_method['settings']['paypal_ec_instance'], array_keys(commerce_paypal_ec_enabled_rules()))) {
      $nvp['NOTIFYURL'] = commerce_paypal_ipn_url('paypal_ec|' . $payment_method['settings']['paypal_ec_instance']);
    }
    else {
      watchdog('commerce_payflow', 'Payflow Link is configured to use a disabled or non-existent Express Checkout payment method rule.', array(), WATCHDOG_WARNING);
    }
  }

  // If reference transactions are supported by the payment method isntance and
  // a billing agreement description is passed in, set its parameters.
  if (!empty($payment_method['settings']['reference_transactions']) && !is_null($billing_agreement)) {
    $nvp['BILLINGTYPE'] = 'MerchantInitiatedBilling';
    if (!empty($billing_agreement)) {
      $nvp['BA_DESC'] = $billing_agreement;
    }
  }

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

  // If the Shipping module is on and we have a shipping address...
  if (module_exists('commerce_shipping') && !empty($order_wrapper->commerce_customer_shipping->commerce_customer_address)) {
    $shipping_address = $order_wrapper->commerce_customer_shipping->commerce_customer_address
      ->value();

    // Add the shipping address parameters to the request.
    $nvp += array(
      'SHIPTOFIRSTNAME' => substr($shipping_address['first_name'], 0, 45),
      'SHIPTOLASTNAME' => substr($shipping_address['last_name'], 0, 45),
      'SHIPTOSTREET' => substr($shipping_address['thoroughfare'], 0, 150),
      'SHIPTOCITY' => substr($shipping_address['locality'], 0, 45),
      'SHIPTOSTATE' => substr($shipping_address['administrative_area'], 0, 2),
      'SHIPTOCOUNTRY' => substr($shipping_address['country'], 0, 2),
      'SHIPTOZIP' => substr($shipping_address['postal_code'], 0, 10),
    );
  }

  // Add the line item details to the array.
  $nvp += commerce_payflow_link_itemize_order($order, $currency_code);

  // Submit the API request to Payflow.
  $response = commerce_payflow_api_request($payment_method, '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 FALSE.
  return FALSE;
}