You are here

function commerce_paypal_checkout_approve_order in Commerce PayPal 7.2

Page callback: Provide the onApprove() callback expected by the SDK.

1 string reference to 'commerce_paypal_checkout_approve_order'
commerce_paypal_checkout_menu in modules/checkout/commerce_paypal_checkout.module
Implements hook_menu().

File

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

Code

function commerce_paypal_checkout_approve_order($order, $payment_method, $flow) {
  $data = drupal_json_decode(file_get_contents('php://input'));
  if (!in_array($flow, array(
    'shortcut',
    'mark',
  )) || !isset($data['id'])) {
    drupal_json_output(array());
    drupal_exit();
  }

  // Store the PayPal order ID, and the "flow" used ("shortcut"|"mark").
  // Note that we don't perform any validation here, that happens inside
  // commerce_paypal_checkout_redirect_form_validate().
  $order->data['commerce_paypal_checkout'] = array(
    'flow' => $flow,
    'remote_id' => $data['id'],
  );

  // The payment_redirect key is required in the payment return url.
  if (empty($order->data['payment_redirect_key'])) {
    $order->data['payment_redirect_key'] = drupal_hash_base64(time());
  }

  // We have to manually set the payment method if empty, it's also required
  // by the payment redirect form validate callback.
  if (empty($order->data['payment_method'])) {
    $order->data['payment_method'] = $payment_method['instance_id'];
  }

  // Update the order status to the payment page for the shortcut flow.
  if ($flow == 'shortcut') {
    commerce_order_status_update($order, 'checkout_payment', FALSE, NULL, t('Customer clicked the Smart payment buttons on the cart page.'));
  }
  else {
    commerce_order_save($order);
  }
  $return_url = url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key']);
  drupal_json_output(array(
    'redirectUri' => $return_url,
  ));
  drupal_exit();
}