updated paypal

This commit is contained in:
franknstayn
2021-10-30 19:20:22 +08:00
parent 9c2a672cc5
commit 066be69216
2 changed files with 186 additions and 149 deletions

View File

@@ -30,7 +30,7 @@ class PaypalController extends Controller
public function __construct() public function __construct()
{ {
$paypal_env = "live"; $paypal_env = "sandbox";
$paypal_apiUrl = 'https://api.paypal.com'; // default $paypal_apiUrl = 'https://api.paypal.com'; // default
if ($paypal_env == 'live') { if ($paypal_env == 'live') {
@@ -88,7 +88,7 @@ class PaypalController extends Controller
$userId = Auth::user()->id; $userId = Auth::user()->id;
$array_address_book = $UserModel->selectAddresBook('UserId', $userId); $array_address_book = $UserModel->selectAddresBook('UserId', $userId);
if(count($array_address_book) <= 0) { if (count($array_address_book) <= 0) {
$message = 'Please complete your shipping address. <a href="user/address-book/create"> <strong> <u>click here</u> </strong></a>.'; $message = 'Please complete your shipping address. <a href="user/address-book/create"> <strong> <u>click here</u> </strong></a>.';
Session::flash('cartkeyError', $message); Session::flash('cartkeyError', $message);
return Redirect::back(); return Redirect::back();
@@ -392,6 +392,22 @@ class PaypalController extends Controller
public function getDone(Request $request) public function getDone(Request $request)
{ {
$paypal_model = new PayPalModel;
$m = new TeamStoreModel;
$cartKey = $request->session()->get('cartkey');
$userId = Auth::user()->id;
$user_email = Auth::user()->email;
$checkCartKey = $paypal_model->checkCartKey($cartKey);
if(count($checkCartKey) > 0) {
$message = 'You already paid for this order. Please check your Order Page. <a href="user/orders"> <strong> <u>click here</u> </strong></a>.';
Session::put('cartkeyError', $message);
return Redirect::route('cart');
}
$id = $request->get('paymentId'); $id = $request->get('paymentId');
$token = $request->get('token'); $token = $request->get('token');
$payer_id = $request->get('PayerID'); $payer_id = $request->get('PayerID');
@@ -405,29 +421,14 @@ class PaypalController extends Controller
$executePayment = $payment->execute($paymentExecution, $this->_apiContext); $executePayment = $payment->execute($paymentExecution, $this->_apiContext);
$obj = json_decode($executePayment); $obj = json_decode($executePayment);
// var_dump($obj); // var_dump($obj);
} catch (PayPalConnectionException $e) {
//throw $th;
// echo $e->getCode();
echo $e->getData();
Session::put('cartkeyError', 'Invalid payment.');
return Redirect::route('cart');
}
if ($executePayment->getState() == 'approved') {
// print_r($executePayment); /** it's all right **/
// if ($executePayment->getState() == 'approved') { /** Here Write your database logic like that insert record or value in database if you want **/
// /** it's all right **/
// /** Here Write your database logic like that insert record or value in database if you want **/
// // \Session::put('success','Payment success');
// // return Redirect::route('paywithpaypal');
// echo 'Payment success';
// }
// var_dump($obj->payer->payer_info->shipping_address);
// var_dump($obj->transactions[0]->item_list->shipping_address);
// \Session::put('success','Payment success');
// return Redirect::route('paywithpaypal');
$line2 = null; $line2 = null;
//details //details
@@ -462,11 +463,7 @@ class PaypalController extends Controller
/// end paypal codes /// end paypal codes
$paypal_model = new PayPalModel;
$m = new TeamStoreModel;
$cartKey = $request->session()->get('cartkey');
$userId = Auth::user()->id;
$user_email = Auth::user()->email;
$payment_details = array( $payment_details = array(
@@ -573,6 +570,32 @@ class PaypalController extends Controller
return view('paypal.get_done') return view('paypal.get_done')
->with('currency', $currency) ->with('currency', $currency)
->with('total', $total); ->with('total', $total);
}else {
Session::put('cartkeyError', 'Something went wrong. Please try again.');
return Redirect::route('cart');
}
} catch (PayPalConnectionException $e) {
//throw $th;
// echo $e->getCode();
echo $e->getData();
Session::put('cartkeyError', 'Invalid payment.');
return Redirect::route('cart');
}
// print_r($executePayment);
// if ($executePayment->getState() == 'approved') {
// /** it's all right **/
// /** Here Write your database logic like that insert record or value in database if you want **/
// // \Session::put('success','Payment success');
// // return Redirect::route('paywithpaypal');
// echo 'Payment success';
// }
// var_dump($obj->payer->payer_info->shipping_address);
// var_dump($obj->transactions[0]->item_list->shipping_address);
} }
@@ -584,6 +607,10 @@ class PaypalController extends Controller
$items = $m->myCart($cartKey); $items = $m->myCart($cartKey);
$getSubtotal = $m->getSubtotal($cartKey); $getSubtotal = $m->getSubtotal($cartKey);
if($items == null) {
return redirect()->route('cart');
}
foreach ($items as $item) { foreach ($items as $item) {
if ($item->VoucherId != null) { if ($item->VoucherId != null) {
$voucherIds[] = $item->VoucherId; $voucherIds[] = $item->VoucherId;

View File

@@ -38,4 +38,14 @@ class PayPalModel extends Model {
// var_dump($i); // var_dump($i);
return $i; return $i;
} }
function checkCartKey($ck){
$i = DB::table('payment_details')
->where('CartKey', $ck)
// ->take(1)
->get();
// var_dump($i);
return $i;
}
} }