You are here

function _uc_stripe_confirm_payment in Ubercart Stripe 7.3

Ajax page callback for callback uc_stripe/ajax/confirm_payment page This is used to send payment and intent status back to JS client

Return value

string Json response

1 string reference to '_uc_stripe_confirm_payment'
uc_stripe_menu in ./uc_stripe.module
Implements hook_menu().

File

./uc_stripe.module, line 1047
A stripe.js PCI-compliant payment gateway Forked from Bitcookie's work (thanks!) which was posted at http://bitcookie.com/blog/pci-compliant-ubercart-and-stripe-js from discussion in the uc_stripe issue queue, https://www.drupal.org/node/1467886

Code

function _uc_stripe_confirm_payment() {
  global $user;

  # retrieve json from POST body
  $received_json = file_get_contents("php://input", TRUE);
  $data = drupal_json_decode($received_json, TRUE);
  $order_id = $data['order_id'];
  $order = uc_order_load($order_id);
  if (!_uc_stripe_prepare_api()) {
    $message = 'Stripe API not found.';
    watchdog('uc_stripe', 'Error in Stripe API: @message', array(
      '@message' => $message,
    ));
    return [
      'error' => $message,
    ];
  }

  // Format the amount in cents, which is what Stripe wants
  $amount = uc_currency_format($order->order_total, FALSE, FALSE, FALSE);
  $stripe_customer_id = False;
  $order_has_stripe_id = key_exists('stripe_customer_id', $order->data) ? True : False;

  // Check various places to get the stripe_customer_id. If not found we'll create
  // a new stripe user.
  if ($order_has_stripe_id) {
    $stripe_customer_id = $order->data['stripe_customer_id'];
  }
  else {
    if ($user->uid != $order->uid) {
      $stripe_customer_id = _uc_stripe_get_customer_id($order->uid);
    }
    else {
      $stripe_customer_id = _uc_stripe_get_customer_id($user->uid);
    }
  }

  // In the case where the stored customer_id is not a valid customer in Stripe
  // then we'll need to create a new stripe customer. see #3071712
  if ($stripe_customer_id && !_uc_stripe_is_stripe_id_valid($stripe_customer_id)) {
    watchdog('uc_stripe', 'Stripe customer: @customer is not valid in this instance of Stripe. A new customer will be created.', array(
      '@customer' => $stripe_customer_id,
    ));
    $stripe_customer_id = false;
  }
  $intent = null;
  try {
    if (isset($data['payment_method_id'])) {
      $payment_method_id = $data['payment_method_id'];
      $params = array(
        'payment_method' => $payment_method_id,
        "description" => t("Order #@order_id", array(
          "@order_id" => $order_id,
        )),
        "metadata" => get_order_metadata($order),
        'amount' => $amount,
        'currency' => strtolower($order->currency),
        'confirmation_method' => 'manual',
        'confirm' => true,
        'setup_future_usage' => 'off_session',
        'save_payment_method' => true,
      );
      if (!$stripe_customer_id) {
        $customer = _uc_stripe_create_stripe_customer($order, $payment_method_id);
        if (!$customer) {
          $message = 'Customer creation failed.';
          return [
            'error' => $message,
          ];
        }
        $stripe_customer_id = $customer->id;
      }
      $params['customer'] = $stripe_customer_id;

      // Idempotency key to mark unique requests in Stripe API.
      $idempotency_key = _uc_stripe_create_idempotency_key($order_id . $amount . $payment_method_id);

      // Allow other modules to alter payment $intent params array
      drupal_alter('uc_stripe_payment_intent', $params, $order);

      # Create the PaymentIntent
      $intent = \Stripe\PaymentIntent::create($params, [
        'idempotency_key' => $idempotency_key,
      ]);
      if (!$order_has_stripe_id) {
        $order->data['stripe_customer_id'] = $stripe_customer_id;
      }
      $order->data['payment_intent_id'] = $intent->id;
      uc_order_save($order);
    }
    if (isset($data['payment_intent_id'])) {
      $intent = \Stripe\PaymentIntent::retrieve($data['payment_intent_id']);
      $intent
        ->confirm();
      $order->data['payment_intent_id'] = $data['payment_intent_id'];
      uc_order_save($order);
    }
    return _generatePaymentResponse($intent);
  } catch (Exception $e) {
    watchdog('uc_stripe', 'Payment could not be processed: @message', array(
      '@message' => $e
        ->getMessage(),
    ));
    return [
      'error' => $e
        ->getMessage(),
    ];
  }
}