You are here

function commerce_paypal_checkout_do_payment in Commerce PayPal 7.2

Capture/authorize a PayPal order and create a payment transaction.

Parameters

$order: The order the payment is for.

$payment_method: The PayPal Checkout payment method instance whose settings should be used to submit the request.

Return value

Boolean indicating the success or failure of the payment request.

2 calls to commerce_paypal_checkout_do_payment()
commerce_paypal_checkout_redirect_form_validate in modules/checkout/commerce_paypal_checkout.module
Payment method callback: redirect form return validation.
commerce_paypal_checkout_review_pane_checkout_form_submit in modules/checkout/includes/commerce_paypal_checkout.checkout_pane.inc
Submit handler for the PayPal Checkout review and confirm page.

File

modules/checkout/commerce_paypal_checkout.module, line 1000
Implements PayPal Checkout in Drupal Commerce checkout.

Code

function commerce_paypal_checkout_do_payment($order, $payment_method) {
  if (empty($order->data['commerce_paypal_checkout']['remote_id'])) {
    return FALSE;
  }
  $paypal_checkout_data = $order->data['commerce_paypal_checkout'];
  $intent = isset($paypal_checkout_data['intent']) ? $paypal_checkout_data['intent'] : $payment_method['settings']['intent'];
  $api_client = commerce_paypal_checkout_api_client($payment_method['settings']);
  try {
    if ($intent == 'capture') {
      $response = $api_client
        ->captureOrder($paypal_checkout_data['remote_id']);
      $remote_payment = $response['purchase_units'][0]['payments']['captures'][0];
    }
    else {
      $response = $api_client
        ->authorizeOrder($paypal_checkout_data['remote_id']);
      $remote_payment = $response['purchase_units'][0]['payments']['authorizations'][0];
    }
  } catch (\Exception $exception) {
    watchdog_exception('commerce_paypal_checkout', $exception);

    // Display an error message and remain on the same page.
    drupal_set_message(t('We could not complete your payment with PayPal. Please try again or contact us if the problem persists.'), 'error');
    watchdog('commerce_paypal_checkout', 'PayPal Checkout transaction failed for order @order_number.', array(
      '@order_number' => $order->order_number,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  $remote_status = strtolower($remote_payment['status']);

  // Prepare a transaction object to log the API response.
  $transaction = commerce_payment_transaction_new('paypal_checkout', $order->order_id);
  $transaction->instance_id = $payment_method['instance_id'];
  $transaction->amount = commerce_currency_decimal_to_amount($remote_payment['amount']['value'], $remote_payment['amount']['currency_code']);
  $transaction->currency_code = $remote_payment['amount']['currency_code'];
  $transaction->payload[REQUEST_TIME] = $response;
  $transaction->remote_id = $remote_payment['id'];
  $transaction->remote_status = $remote_payment['status'];

  // Store the transaction ID as the parent transaction ID in case subsequent
  // API operations alter this transaction's remote ID.
  if (!empty($transaction->remote_id)) {
    $transaction->data['commerce_paypal_checkout']['original_remote_id'] = $transaction->remote_id;
  }
  if (in_array($remote_status, [
    'denied',
    'expired',
    'declined',
  ])) {
    $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
    commerce_payment_transaction_save($transaction);

    // Display an error message and remain on the same page.
    drupal_set_message(t('We could not complete your payment with PayPal. Please try again or contact us if the problem persists.'), 'error');
    watchdog('commerce_paypal_checkout', 'PayPal Checkout transaction failed for order @order_number.', array(
      '@order_number' => $order->order_number,
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // Map the remote status to a Drupal commerce payment status.
  $status_mapping = array(
    'created' => COMMERCE_PAYMENT_STATUS_PENDING,
    'pending' => COMMERCE_PAYMENT_STATUS_PENDING,
    'completed' => COMMERCE_PAYMENT_STATUS_SUCCESS,
  );

  // If we do not know how to handle this remote payment status, set the payment
  // status to failure and stop here.
  if (!isset($status_mapping[$remote_status])) {
    $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
    commerce_payment_transaction_save($transaction);

    // Display an error message and remain on the same page.
    drupal_set_message(t('We could not complete your payment with PayPal. Please try again or contact us if the problem persists.'), 'error');
    watchdog('commerce_paypal_checkout', 'PayPal Checkout transaction failed for order @order_number.', array(
      '@order_number' => $order->order_number,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  $transaction->status = $status_mapping[$remote_status];
  commerce_payment_transaction_save($transaction);
  return TRUE;
}