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()
{
$paypal_env = "live";
$paypal_env = "sandbox";
$paypal_apiUrl = 'https://api.paypal.com'; // default
if ($paypal_env == 'live') {
@@ -88,7 +88,7 @@ class PaypalController extends Controller
$userId = Auth::user()->id;
$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>.';
Session::flash('cartkeyError', $message);
return Redirect::back();
@@ -392,6 +392,22 @@ class PaypalController extends Controller
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');
$token = $request->get('token');
$payer_id = $request->get('PayerID');
@@ -405,6 +421,159 @@ class PaypalController extends Controller
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
$obj = json_decode($executePayment);
// var_dump($obj);
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');
$line2 = null;
//details
$total = $obj->transactions[0]->amount->total;
$sub_total = $obj->transactions[0]->amount->details->subtotal;
$tax = $obj->transactions[0]->amount->details->tax;
$shipping = $obj->transactions[0]->amount->details->shipping;
$relatedResources = $obj->transactions[0]->related_resources[0];
$saleId = $relatedResources->sale->id; // transaction_id
$currency = $obj->transactions[0]->amount->currency;
$invoice_number = $obj->transactions[0]->invoice_number;
// var_dump( $obj->transactions[0]->item_list->phone);
//shipping address details
$recipient_name = $obj->transactions[0]->item_list->shipping_address->recipient_name;
$line1 = $obj->transactions[0]->item_list->shipping_address->line1;
if (isset($obj->transactions[0]->item_list->shipping_address->line2)) {
$line2 = $obj->transactions[0]->item_list->shipping_address->line2;
}
$city = $obj->transactions[0]->item_list->shipping_address->city;
$state = $obj->transactions[0]->item_list->shipping_address->state;
$postal_code = $obj->transactions[0]->item_list->shipping_address->postal_code;
$country_code = $obj->transactions[0]->item_list->shipping_address->country_code;
// payer info
$payment_method = $obj->payer->payment_method;
$email = $obj->payer->payer_info->email;
$first_name = $obj->payer->payer_info->first_name;
$last_name = $obj->payer->payer_info->last_name;
$_payer_id = $obj->payer->payer_info->payer_id;
/// end paypal codes
$payment_details = array(
'UserId' => $userId,
'CartKey' => $cartKey,
'PaymentId' => $id,
'Token' => $token,
'PayerId' => $payer_id,
'InvoiceNumber' => $invoice_number,
'Currency' => $currency,
'Total' => $total,
'SubTotal' => $sub_total,
'Tax' => $tax,
'Payer_Email' => $email,
'Payer_Firstname' => $first_name,
'Payer_Lastname' => $last_name,
'PaymentMethod' => $payment_method,
'ShippingCost' => $shipping,
'TransactionId' => $saleId
);
$p_id = $paypal_model->insertToPaypalDetails($payment_details);
$shipping_address = array(
'PaymentDetail_Id' => $p_id,
'recipient_name' => $recipient_name,
'line1' => $line1,
'line2' => $line2,
'city' => $city,
'state' => $state,
'postal_code' => $postal_code,
'country_code' => $country_code,
);
// iinsert shipping address
$paypal_model->insertShippingAddress($shipping_address);
// insert order from cart_tmp to orders table
$l = $paypal_model->insertToOrders($cartKey); // insert to orders table
//email sending
$newUserModel = new UserModel;
$order_item_array = $newUserModel->selectOrderItem($cartKey);
$item_goup_array = $newUserModel->itemGroup($cartKey);
$item_thumbs = $newUserModel->selectDisplayItemThumb();
$array_payment_details = $newUserModel->selectPaymentDetails('CartKey', $cartKey);
$array_storename = $newUserModel->selectTeamStoreName($cartKey); // email subject
foreach ($array_storename as $storname) {
$sName[] = $storname->StoreName;
$sid[] = $storname->Id;
}
$sName = implode(", ", $sName);
$user_loginsArray = $newUserModel->selectUserLoginsWhereIn($sid);
foreach ($user_loginsArray as $userdata) {
$other_email[] = $userdata->other_email;
}
if ($other_email[0] != null) {
$other_email = implode(", ", $other_email);
$email_cc = "orders@merchbay.com" . "," . $other_email;
} else {
$email_cc = "orders@merchbay.com";
}
$explode_other_email = explode(",", $email_cc);
$data = array(
'order_item_array' => $order_item_array,
'item_goup_array' => $item_goup_array,
'img_thumb' => $item_thumbs,
'array_payment_details' => $array_payment_details,
'receiver' => $user_email,
'email_cc' => $explode_other_email,
'subject' => $sName . ' ORDERS',
);
Mail::send('emails.orders', $data, function ($message) use ($data) {
$message->from('support@merchbay.com', 'Merchbay');
$message->bcc($data['email_cc'], 'Orders From Merchbay');
$message->to($data['receiver'])->subject($data['subject']);
});
// end email sending
$insertTracking = array(
"StepId" => 1,
"ScannedBy" => 1,
"InvoiceNumber" => $invoice_number,
"created_at" => date('Y-m-d H:i:s')
);
$ApiModel = new ApiModel;
$ApiModel->insertTracking($insertTracking);
$request->session()->forget('cartkey'); // clear session for cartkey
// redirect to thank you page.
return view('paypal.get_done')
->with('currency', $currency)
->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();
@@ -427,152 +596,6 @@ class PaypalController extends Controller
// var_dump($obj->payer->payer_info->shipping_address);
// var_dump($obj->transactions[0]->item_list->shipping_address);
$line2 = null;
//details
$total = $obj->transactions[0]->amount->total;
$sub_total = $obj->transactions[0]->amount->details->subtotal;
$tax = $obj->transactions[0]->amount->details->tax;
$shipping = $obj->transactions[0]->amount->details->shipping;
$relatedResources = $obj->transactions[0]->related_resources[0];
$saleId = $relatedResources->sale->id; // transaction_id
$currency = $obj->transactions[0]->amount->currency;
$invoice_number = $obj->transactions[0]->invoice_number;
// var_dump( $obj->transactions[0]->item_list->phone);
//shipping address details
$recipient_name = $obj->transactions[0]->item_list->shipping_address->recipient_name;
$line1 = $obj->transactions[0]->item_list->shipping_address->line1;
if (isset($obj->transactions[0]->item_list->shipping_address->line2)) {
$line2 = $obj->transactions[0]->item_list->shipping_address->line2;
}
$city = $obj->transactions[0]->item_list->shipping_address->city;
$state = $obj->transactions[0]->item_list->shipping_address->state;
$postal_code = $obj->transactions[0]->item_list->shipping_address->postal_code;
$country_code = $obj->transactions[0]->item_list->shipping_address->country_code;
// payer info
$payment_method = $obj->payer->payment_method;
$email = $obj->payer->payer_info->email;
$first_name = $obj->payer->payer_info->first_name;
$last_name = $obj->payer->payer_info->last_name;
$_payer_id = $obj->payer->payer_info->payer_id;
/// 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(
'UserId' => $userId,
'CartKey' => $cartKey,
'PaymentId' => $id,
'Token' => $token,
'PayerId' => $payer_id,
'InvoiceNumber' => $invoice_number,
'Currency' => $currency,
'Total' => $total,
'SubTotal' => $sub_total,
'Tax' => $tax,
'Payer_Email' => $email,
'Payer_Firstname' => $first_name,
'Payer_Lastname' => $last_name,
'PaymentMethod' => $payment_method,
'ShippingCost' => $shipping,
'TransactionId' => $saleId
);
$p_id = $paypal_model->insertToPaypalDetails($payment_details);
$shipping_address = array(
'PaymentDetail_Id' => $p_id,
'recipient_name' => $recipient_name,
'line1' => $line1,
'line2' => $line2,
'city' => $city,
'state' => $state,
'postal_code' => $postal_code,
'country_code' => $country_code,
);
// iinsert shipping address
$paypal_model->insertShippingAddress($shipping_address);
// insert order from cart_tmp to orders table
$l = $paypal_model->insertToOrders($cartKey); // insert to orders table
//email sending
$newUserModel = new UserModel;
$order_item_array = $newUserModel->selectOrderItem($cartKey);
$item_goup_array = $newUserModel->itemGroup($cartKey);
$item_thumbs = $newUserModel->selectDisplayItemThumb();
$array_payment_details = $newUserModel->selectPaymentDetails('CartKey', $cartKey);
$array_storename = $newUserModel->selectTeamStoreName($cartKey); // email subject
foreach ($array_storename as $storname) {
$sName[] = $storname->StoreName;
$sid[] = $storname->Id;
}
$sName = implode(", ", $sName);
$user_loginsArray = $newUserModel->selectUserLoginsWhereIn($sid);
foreach ($user_loginsArray as $userdata) {
$other_email[] = $userdata->other_email;
}
if ($other_email[0] != null) {
$other_email = implode(", ", $other_email);
$email_cc = "orders@merchbay.com" . "," . $other_email;
} else {
$email_cc = "orders@merchbay.com";
}
$explode_other_email = explode(",", $email_cc);
$data = array(
'order_item_array' => $order_item_array,
'item_goup_array' => $item_goup_array,
'img_thumb' => $item_thumbs,
'array_payment_details' => $array_payment_details,
'receiver' => $user_email,
'email_cc' => $explode_other_email,
'subject' => $sName . ' ORDERS',
);
Mail::send('emails.orders', $data, function ($message) use ($data) {
$message->from('support@merchbay.com', 'Merchbay');
$message->bcc($data['email_cc'], 'Orders From Merchbay');
$message->to($data['receiver'])->subject($data['subject']);
});
// end email sending
$insertTracking = array(
"StepId" => 1,
"ScannedBy" => 1,
"InvoiceNumber" => $invoice_number,
"created_at" => date('Y-m-d H:i:s')
);
$ApiModel = new ApiModel;
$ApiModel->insertTracking($insertTracking);
$request->session()->forget('cartkey'); // clear session for cartkey
// redirect to thank you page.
return view('paypal.get_done')
->with('currency', $currency)
->with('total', $total);
}
@@ -584,6 +607,10 @@ class PaypalController extends Controller
$items = $m->myCart($cartKey);
$getSubtotal = $m->getSubtotal($cartKey);
if($items == null) {
return redirect()->route('cart');
}
foreach ($items as $item) {
if ($item->VoucherId != null) {
$voucherIds[] = $item->VoucherId;