Esempio n. 1
0
 public function handlePayment(Request $request)
 {
     if (Auth::check()) {
         $user = Auth::user();
     } else {
         $userSearch = User::where('email', $request->email)->first();
         if (!$userSearch) {
             $user = User::create(['name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->email)]);
         } else {
             $user = $userSearch;
         }
     }
     $product = Product::find($request->product_id);
     $amount = number_format($product->price / 100, 2);
     switch ($request->provider) {
         case 'stripe':
             $gateway = Omnipay::create('Stripe');
             $token = $request->stripeToken;
             $params = ['amount' => $amount, 'currency' => 'USD', 'token' => $token, 'payment_type' => 'stripe', 'receipt_email' => $user->email, 'description' => $product->name];
             $gateway->setApiKey(ENV('STRIPE_SECRET'));
             break;
         case 'paypal':
             $gateway = Omnipay::create('PayPal_Express');
             $params = ['cancelUrl' => 'https://makerscabin.com/purchase', 'returnUrl' => ENV('PAYPAL_RETURN'), 'description' => $product->name, 'amount' => $amount, 'currency' => 'USD', 'product_id' => $product->id, 'payment_type' => 'paypal'];
             $gateway->setUsername(ENV('PAYPAL_USERNAME'));
             $gateway->setPassword(ENV('PAYPAL_PASSWORD'));
             $gateway->setSignature(ENV('PAYPAL_SIGNATURE'));
             $gateway->setTestMode(ENV('PAYPAL_TEST'));
             Session::put('params', $params);
             Session::save();
             break;
         default:
             # code...
             break;
     }
     $payment = $gateway->purchase($params);
     $data = $payment->getData();
     $data['receipt_email'] = $user->email;
     $data['metadata'] = ['email' => $user->email, 'user_id' => $user->id];
     $response = $payment->sendData($data);
     if ($response->isSuccessful()) {
         $this->dispatch(new SendPurchaseConfirmationEmail($user, $product));
         $order = Order::create(['user_id' => $user->id, 'product_id' => $product->id, 'payment_method' => 'stripe', 'amount' => $product->price]);
         return redirect()->route('receipt', $product->slug)->withSuccess('Your purchase of ' . $product->name . ' was successful.');
     } elseif ($response->isRedirect()) {
         $response->redirect();
     } else {
         return redirect('purchase')->withErrors([$response->getMessage()]);
     }
 }
Esempio n. 2
0
 public function history()
 {
     $orders = Order::where('user_id', Auth::user()->id)->get();
     return view('user.history', compact('orders'));
 }