diff --git a/src/Plugin/Ubercart/PaymentMethod/StripeGateway.php b/src/Plugin/Ubercart/PaymentMethod/StripeGateway.php index 3282ff9..3cb9f03 100644 --- a/src/Plugin/Ubercart/PaymentMethod/StripeGateway.php +++ b/src/Plugin/Ubercart/PaymentMethod/StripeGateway.php @@ -193,6 +193,7 @@ class StripeGateway extends CreditCardPaymentMethodBase { * {@inheritdoc} */ protected function chargeCard(OrderInterface $order, $amount, $txn_type, $reference = NULL) { + $user = \Drupal::currentUser(); if (!$this->prepareApi()) { @@ -219,7 +220,6 @@ class StripeGateway extends CreditCardPaymentMethodBase { $stripe_customer_id = $this->getStripeCustomerID($order->id()); } - // Always Create a new customer in stripe for new orders if (!$stripe_customer_id) { @@ -265,21 +265,84 @@ class StripeGateway extends CreditCardPaymentMethodBase { return $result; } - $message = $this->t('Credit card charged: @amount', ['@amount' => uc_currency_format($amount)]); - uc_order_comment_save($order->id(), $user->id(), $message, 'admin'); + // Charge the customer + + //--Handle transactions for $0 + + // Stripe can't handle transactions < $0.50, but $0 is a common value + // so we will just return a positive result when the amount is $0. + if ($amount == 0) { + $result = array( + 'success' => TRUE, + 'message' => t('Payment of 0 approved'), + 'uid' => $user->uid, + 'trans_id' => md5(uniqid(rand())), + ); + uc_order_comment_save($order->id(), $user->id(), $result['message'], 'admin'); + return $result; + } + + try { + // charge the Customer the amount in the order + $currency = \Drupal::config('uc_store.settings')->get('currency')['code']; + $charge = \Stripe\Charge::create(array( + "amount" => $amount, + "currency" => strtolower($currency), + "customer" => $customer->id, + "description" => t("Order #@order_id", array("@order_id" => $order->id())), + ) + ); + + $formatted_amount = $amount / 100; + $formatted_amount = number_format($formatted_amount, 2); + + $message = $this->t('Credit card charged: @amount', ['@amount' => $formatted_amount]); + uc_order_comment_save($order->id(), $user->id(), $message, 'admin'); + + $result = array( + 'success' => TRUE, + 'comment' => $this->t('Card charged, resolution code: 0022548315'), + 'message' => $this->t('Credit card payment processed successfully.'), + 'uid' => $user->id(), + ); + + return $result; + + } catch (Exception $e) { + $result = array( + 'success' => FALSE, + 'comment' => $e->getCode(), + 'message' => t("Stripe Charge Failed for order !order: !message", array( + "!order" => $order->id(), + "!message" => $e->getMessage() + )), + 'uid' => $user->uid, + 'order_id' => $order->id(), + ); + uc_order_comment_save($order->id(), $user->uid, $result['message'], 'admin'); + watchdog('uc_stripe', 'Stripe charge failed for order @order, message: @message', array('@order' => $order->id(), '@message' => $result['message'])); + + return $result; + } + + // Default / Fallback procedure to fail if the above conditions aren't met $result = array( - 'success' => TRUE, - 'comment' => $this->t('Card charged, resolution code: 0022548315'), - 'message' => $this->t('Credit card payment processed successfully.'), - 'uid' => $user->id(), + 'success' => FALSE, + 'comment' => "Stripe Gateway Error", + 'message' => "Stripe Gateway Error", + 'uid' => $user->uid, + 'order_id' => $order->id(), ); + uc_order_comment_save($order->id(), $user->uid, $result['message'], 'admin'); + + watchdog('uc_stripe', 'Stripe gateway error for order @order_id', array('order_id' => $order->id())); + return $result; } } - /** * Utility function: Load stripe API *