From d9c558a82975df91c536863e34279c345d3d273a Mon Sep 17 00:00:00 2001 From: franknstayn Date: Thu, 5 Aug 2021 23:13:50 +0800 Subject: [PATCH] update paypal payment --- .../Controllers/paypal/PaypalController.php | 35 ++++++++++++++----- resources/views/merchbay/cart.blade.php | 9 +++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/paypal/PaypalController.php b/app/Http/Controllers/paypal/PaypalController.php index 6f51fc0..3776f6c 100755 --- a/app/Http/Controllers/paypal/PaypalController.php +++ b/app/Http/Controllers/paypal/PaypalController.php @@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Redirect; // use Mail; use Illuminate\Support\Facades\Mail; +use PayPal\Exception\PayPalConnectionException; class PaypalController extends Controller @@ -28,11 +29,10 @@ class PaypalController extends Controller public function __construct() { - $paypal_env = "live"; + $paypal_env = "sandbox"; $paypal_apiUrl = 'https://api.paypal.com'; // default if($paypal_env == 'live'){ - $paypal_apiUrl = 'https://api.paypal.com'; }else{ $paypal_apiUrl = 'https://api.sandbox.paypal.com'; @@ -97,7 +97,14 @@ class PaypalController extends Controller $m = new TeamStoreModel; $paypal_model = new PayPalModel; $last_id = $paypal_model->getLastIdPaymentDetails(); - $invoice_num = str_pad($last_id[0]->Id, 4, '0', STR_PAD_LEFT); + // var_dump(count($last_id)); + if(count($last_id) > 0){ + $lastId = $last_id[0]->Id + 1; + }else{ + $lastId = 1; + } + $invoice_num = str_pad($lastId, 6, '0', STR_PAD_LEFT); + // var_dump($invoice_num); $cartKey = $request->session()->get('cartkey'); $items = $m->myCart($cartKey); @@ -252,13 +259,23 @@ class PaypalController extends Controller $token = $request->get('token'); $payer_id = $request->get('PayerID'); + try { + $payment = PayPal::getById($id, $this->_apiContext); + + $paymentExecution = PayPal::PaymentExecution(); + + $paymentExecution->setPayerId($payer_id); + $executePayment = $payment->execute($paymentExecution, $this->_apiContext); + $obj = json_decode($executePayment); - $payment = PayPal::getById($id, $this->_apiContext); + } catch (PayPalConnectionException $e) { + //throw $th; + // echo $e->getCode(); + echo $e->getData(); + Session::put('cartkeyError','Invalid payment.'); + return Redirect::route('cart'); + } - $paymentExecution = PayPal::PaymentExecution(); - - $paymentExecution->setPayerId($payer_id); - $executePayment = $payment->execute($paymentExecution, $this->_apiContext); // print_r($executePayment); // if ($executePayment->getState() == 'approved') { @@ -270,7 +287,7 @@ class PaypalController extends Controller // // return Redirect::route('paywithpaypal'); // echo 'Payment success'; // } - $obj = json_decode($executePayment); + $line2 = null; diff --git a/resources/views/merchbay/cart.blade.php b/resources/views/merchbay/cart.blade.php index 6f5fd07..14125b5 100755 --- a/resources/views/merchbay/cart.blade.php +++ b/resources/views/merchbay/cart.blade.php @@ -40,11 +40,16 @@

Shopping Cart

@if(Session::has('cartkeyError')) -
+ {{--

Error:

{!! Session::get('cartkeyError') !!} -
+
--}} + + @endif