Example #1
0
 public function getSuccessPayment()
 {
     $gateway = Omnipay::create('PayPal_Express');
     $gateway->setUsername(ENV('PAYPAL_USERNAME'));
     $gateway->setPassword(ENV('PAYPAL_PASSWORD'));
     $gateway->setSignature(ENV('PAYPAL_SIGNATURE'));
     $gateway->setTestMode(ENV('PAYPAL_TEST'));
     $params = Session::get('params');
     $response = $gateway->completePurchase($params)->send();
     $paypalResponse = $response->getData();
     if (isset($paypalResponse['PAYMENTINFO_0_ACK']) && $paypalResponse['PAYMENTINFO_0_ACK'] === 'Success') {
         $user = Auth::user();
         // Assign Product to User
         $product = Product::find($params['product_id']);
         $user->products()->save($product, ['payment_type' => $params['payment_type'], 'amount' => $params['amount']]);
         foreach ($product->courses as $course) {
             $user->courses()->save($course);
         }
         // Assign Student Role to User
         if (!$user->hasRole('starter')) {
             $user->assignRole('starter');
         }
         $this->dispatch(new SendPurchaseConfirmationEmail($user, $product));
         return redirect()->route('thanks', $product->slug)->withSuccess('Your purchase of ' . $product->name . ' was successful.');
     } else {
         return redirect('purchase')->withErrors('Purchase failed.');
     }
 }
Example #2
0
 public function handlePayment(Request $request)
 {
     $amount = 0;
     $product = Product::find($request->product_id);
     $amount = $amount + $product->price;
     $amount = number_format($amount / 100, 2);
     switch ($request->provider) {
         case 'stripe':
             $gateway = Omnipay::create('Stripe');
             $token = $request->stripeToken;
             $params = ['amount' => $amount, 'currency' => 'USD', 'token' => $token, 'payment_type' => 'stripe', 'email' => Auth::user()->email, 'description' => $product->name];
             $gateway->setApiKey(ENV('STRIPE_SECRET'));
             break;
         case 'paypal':
             $gateway = Omnipay::create('PayPal_Express');
             $params = ['cancelUrl' => 'https://40daystartup.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;
     }
     //$response = $gateway->purchase($params)->send();
     $payment = $gateway->purchase($params);
     $data = $payment->getData();
     $data['receipt_email'] = Auth::user()->email;
     $data['metadata'] = ['email' => Auth::user()->email, 'user_id' => Auth::user()->id];
     $response = $payment->sendData($data);
     if ($response->isSuccessful()) {
         $user = Auth::user();
         // Assign Product to User
         $user->products()->save($product, ['payment_type' => $params['payment_type'], 'amount' => $params['amount']]);
         foreach ($product->courses as $course) {
             $user->courses()->save($course);
         }
         // Assign Student Role to User
         if (!$user->hasRole('starter')) {
             $user->assignRole('starter');
         }
         // Fire off purchase email
         $this->dispatch(new SendPurchaseConfirmationEmail($user, $product));
         Session::flush();
         return redirect()->route('thanks', $product->slug)->withSuccess('Your purchase of ' . $product->name . ' was successful.');
     } elseif ($response->isRedirect()) {
         $response->redirect();
     } else {
         return redirect('purchase')->withErrors([$response->getMessage()]);
     }
 }