Exemple #1
0
 /**
  * HTTP accessible method
  * @param $args
  * @return array
  * @throws \Exception
  * @throws \Stripe\Error\Card
  */
 public function submitFormHandler($args)
 {
     $stripe = new StripeFacade();
     // Is the basket ready for a transaction? Or has the transaction
     // already occurred? If not then refuse to process
     // This is just a backup as the basket won't show the stripe button
     // if it's not ready or the transaction is complete.
     if (!$this->state->ready || $this->state->complete) {
         $this->parent->setFlashError("The basket is not ready yet. Please ensure you've filled in all required fields.");
         $root = $this->getRootComponent();
         $root->updateState();
         return $root->render();
     }
     $this->testInputs(['stripeToken' => ["string"]], $args);
     $c = (object) $this->state->config;
     $stripeToken = $args['stripeToken'];
     $apiPrivKey = $this->state->testMode ? $c->testApiPrivKey : $c->liveApiPrivKey;
     $stripe->setApiKey($apiPrivKey);
     $tok = $stripe->tokenRetrieve($stripeToken, $apiPrivKey);
     $paymentCountryCode = '';
     $paymentType = "";
     if ($tok->type == 'card') {
         $paymentCountryCode = $tok->card->country;
         $paymentType = "card";
     }
     if ($tok->type == 'bank_account') {
         $paymentCountryCode = $tok->bank_account->country;
         $paymentType = "bank_account";
     }
     // Do we require VAT location proof, and if so do we have
     // enough and does it match the information used
     // to calculate the original VAT?
     if (!$this->parent->confirmValidTxnFunc($paymentCountryCode)) {
         $this->parent->setFlashError("Sorry but we can't collect enough information about your location to comply with EU VAT legislation with the information we have available. You have not been charged. Please contact us to arrange a manual payment.");
         $root = $this->getRootComponent();
         $root->updateState();
         return $root->render();
     }
     /*
              Stripe_Token Object
             (
                 [_apiKey:protected] => sk_test_xxx
                 [_values:protected] => Array
                 (
                     [id] => tok_xxx
                     [livemode] =>
                     [created] => 141xxx
                     [used] =>
                     [object] => token
                     [type] => card
                     [card] => Stripe_Card Object
                         (
                         [_apiKey:protected] => sk_test_xxx
                         [_values:protected] => Array
                             (
                             [id] => card_xxx
                             [object] => card
                             [last4] => xxx
                             [brand] => Visa
                             [funding] => credit
                             [exp_month] => 1
                             [exp_year] => 2016
                             [fingerprint] => xxx
                             [country] => US
                             [name] => xxx@xxx.org
                             [address_line1] =>
                             [address_line2] =>
                             [address_city] =>
                             [address_state] =>
                             [address_zip] =>
                             [address_country] =>
                             [cvc_check] =>
                             [address_line1_check] =>
                             [address_zip_check] =>
                             [dynamic_last4] =>
                             [customer] =>
     */
     $amount = $this->state->amount;
     $currency = $c->currency;
     $description = $this->state->description;
     try {
         if ($this->state->chargeMode == "immediate") {
             // Create the charge on Stripe's servers - this will charge the user's card
             $ret = $this->chargeCard($stripe, $stripeToken, $amount, $currency, $description, $paymentCountryCode, $paymentType);
         } else {
             // Generate token for later/repeat charge
             $ret = $this->getDelayedOrRepeatPaymentTransaction($stripe, $stripeToken, $paymentCountryCode, $paymentType);
         }
     } catch (\Stripe\Error\Card $e) {
         $this->parent->setFlashError("Sorry but there was a problem authorising your transaction. The payment provider said: '{$e->getMessage()}'");
         $root = $this->getRootComponent();
         $root->updateState();
         $ret = $root->render();
     }
     return $ret;
 }