/** * @param $stripeToken * @param $apiPrivKey * @return StripeTokenMock|Token */ function tokenRetrieve($stripeToken, $apiPrivKey) { if (self::$testMode) { return StripeTokenMock::retrieve($stripeToken, $apiPrivKey); } else { return Token::retrieve($stripeToken, $apiPrivKey); } }
protected function collectOneTimePayment($token, $amount) { Stripe::setApiKey(config('services.stripe.secret')); $user = Auth::user(); $customer = Customer::create(['source' => $token, 'email' => $user->email, 'description' => 'Pre-launch customer']); Charge::create(['amount' => $amount, 'currency' => 'usd', 'customer' => $customer->id]); /* * Save the strip_id and card details to the DB. */ $tokenDetails = Token::retrieve($token); $user->stripe_id = $customer->id; $user->card_brand = $tokenDetails->card->brand; $user->card_last_four = $tokenDetails->card->last4; $user->save(); }
public function submitOrder($short_name) { $university = $this->university->where('short_name', $short_name)->first(); $firstName = $this->request->input('first_name', ''); $lastName = $this->request->input('last_name', ''); $emainAddress = $this->request->input('email_address', ''); $customer = $this->customer->where('first_name', $firstName)->where('last_name', $lastName)->where('email_address', $emainAddress)->first(); $customerId; if ($customer === null) { //This customer is new $this->customer->fill($this->request->all())->save(); $customerId = $this->customer->id; } else { $this->customer->find($customer->id)->fill($this->request->all())->save(); $customerId = $customer->id; } $totalPrice = $this->request->input('totalPrice', ''); $orderId = $this->order->insertGetId(['customer_id' => $customerId, 'payment' => $this->request->input('payment', ''), 'order_status' => "initialized", 'street_address1' => $this->request->input('street_address1', ''), 'street_address2' => $this->request->input('street_address2', ''), 'city' => $this->request->input('city', ''), 'post_code' => $this->request->input('post_code', ''), 'country' => $this->request->input('country', ''), 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'total_price' => $totalPrice]); $itemsInCart = $this->cart->where('customer_id', $this->request->cookie('customer_id'))->get(); foreach ($itemsInCart as $item) { $this->orderitem->insert(['order_id' => $orderId, 'product_id' => $item->product_id, 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'price' => $item->price]); $cart = $this->cart->find($item->id); $cart->order_id = $orderId; $cart->save(); } $temporaryCustomerId = $this->request->cookie('customer_id'); $allProductsInCart = $this->getAllProductsInCart($temporaryCustomerId); $allProductsInCart = $this->convertProductsForCheckout($allProductsInCart); \Stripe\Stripe::setApiKey("sk_test_I561ILtdEXsIVDoAc9169tcC"); $transactionInformation = \Stripe\Charge::all(); $transactionId = $transactionInformation->data[0]->id; $token = $this->request->input('stripeToken', ''); $tokenInformation = \Stripe\Token::retrieve($token); $chargeInformation = \Stripe\Charge::retrieve($transactionId); //dd($transactionInformation); try { $charge = \Stripe\Charge::create(array("amount" => bcmul($totalPrice, 100), "currency" => "aud", "source" => $token, "description" => "Example charge")); $type = $tokenInformation->type; $clientIp = $tokenInformation->client_ip; $createdTime = $chargeInformation->created; $country = $chargeInformation->source->country; $expMonth = $chargeInformation->source->exp_month; $expYear = $chargeInformation->source->exp_year; $brand = $chargeInformation->source->brand; $last4CardNumber = $chargeInformation->source->last4; $paymentResponse = json_encode(array('type' => $type, 'client_ip' => $clientIp, 'created' => $createdTime, 'country' => $country, 'expMonth' => $expMonth, 'expYear' => $expYear, 'brand' => $brand, 'last4CardNumber' => $last4CardNumber)); $transactionId = \Stripe\Charge::all()->data[0]->id; $order = $this->order->find($orderId); $order->transaction_id = $transactionId; $order->order_status = "paid"; $order->payment_response = $paymentResponse; $order->save(); $shippingFee = $this->request->input('shipping_fee', ''); $this->sendConfirmationMail($customerId, $allProductsInCart, $totalPrice, $shippingFee); foreach ($allProductsInCart as $productInCart) { $product = $this->product->find($productInCart['id']); $product->stock -= $productInCart['quantity']; $product->save(); if ($product->stock < 0) { $this->sendStockShortageNotification($product, $orderId); $order->order_status = "out_of_stock"; $order->save(); } } foreach ($itemsInCart as $item) { $this->cart->destroy($item->id); } return view('www.orderSubmitted', ['template' => $university->template, 'basket' => $this->getTheNumberOfItems(), 'products' => $allProductsInCart, 'totalPrice' => $totalPrice, 'shippingFee' => $shippingFee, 'headerLinks' => $this->headerPages, 'footerLinks' => $this->footerPages]); } catch (\Stripe\Error\Card $e) { $type = $tokenInformation->type; $clientIp = $tokenInformation->client_ip; $createdTime = $chargeInformation->created; $country = $chargeInformation->source->country; $expMonth = $chargeInformation->source->exp_month; $expYear = $chargeInformation->source->exp_year; $brand = $chargeInformation->source->brand; $last4CardNumber = $chargeInformation->source->last4; $failureCode = $chargeInformation->failure_code; $failureMessage = $chargeInformation->failure_message; $paymentResponse = json_encode(array('type' => $type, 'client_ip' => $clientIp, 'created' => $createdTime, 'country' => $country, 'expMonth' => $expMonth, 'expYear' => $expYear, 'brand' => $brand, 'last4CardNumber' => $last4CardNumber, 'failureCode' => $failureCode, 'failureMessage' => $failureMessage)); $order = $this->order->find($orderId); $order->order_status = "failed"; $order->payment_response = $paymentResponse; $order->save(); include 'countries.php'; $isShipping = false; foreach ($allProductsInCart as $productInCart) { if ($productInCart['is_shipping'] === "Yes" && $isShipping === false) { $isShipping = true; } } $domesticShippingFee = $this->setting->where('key', 'domesticShippingFee')->first(); $domesticShippingFee = json_decode($domesticShippingFee->json)->data; $overseasShippingFee = $this->setting->where('key', 'overseasShippingFee')->first(); $overseasShippingFee = json_decode($overseasShippingFee->json)->data; $message = "Unfortunately, the submission has been failed :" . $failureCode; return view('www.checkout', ['template' => $university->template, 'universityShortName' => $short_name, 'basket' => $this->getTheNumberOfItems(), 'products' => $allProductsInCart, 'totalPrice' => $totalPrice, 'countries' => $countries, 'is_shipping' => $isShipping, 'domesticShippingFee' => $domesticShippingFee, 'overseasShippingFee' => $overseasShippingFee, 'message' => $message, 'headerLinks' => $this->headerPages, 'footerLinks' => $this->footerPages]); } }
/** * Update customer's credit card. * * @param string $token */ public function updateCard($token) { $customer = $this->asStripeCustomer(); $token = Token::retrieve($token, ['api_key' => $this->getStripeKey()]); // If the given token already has the card as their default source, we can just // bail out of the method now. We don't need to keep adding the same card to // the user's account each time we go through this particular method call. if ($token->card->id === $customer->default_source) { return; } $card = $customer->sources->create(['source' => $token]); $customer->default_source = $card->id; $customer->save(); // Next, we will get the default source for this user so we can update the last // four digits and the card brand on this user record in the database, which // is convenient when displaying on the front-end when updating the cards. $source = $customer->default_source ? $customer->sources->retrieve($customer->default_source) : null; $this->fillCardDetails($source); $this->save(); }
/** * Get the country code for the given Stripe token. * * @param string $token * @return string */ public function countryForToken($token) { return StripeToken::retrieve($token, config('services.stripe.secret'))->card->country; }