updated checkout flow
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\paypal;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
// use Paypal;
|
||||
use Netshell\Paypal\Facades\Paypal;
|
||||
@@ -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,30 +29,42 @@ class PaypalController extends Controller
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$paypal_env = "live";
|
||||
$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';
|
||||
}
|
||||
|
||||
$this->_apiContext = PayPal::ApiContext(
|
||||
config('services.paypal.client_id'),
|
||||
config('services.paypal.secret')
|
||||
config('services.paypal_' . $paypal_env . '.client_id'),
|
||||
config('services.paypal_' . $paypal_env . '.secret')
|
||||
|
||||
// config('services.paypal_live.client_id'),
|
||||
// config('services.paypal_live.secret')
|
||||
);
|
||||
|
||||
|
||||
// $this->_apiContext->setConfig(array(
|
||||
// 'mode' => 'sandbox',
|
||||
// 'service.EndPoint' => 'https://api.sandbox.paypal.com',
|
||||
// 'http.ConnectionTimeOut' => 30,
|
||||
// 'log.LogEnabled' => true,
|
||||
// 'log.FileName' => storage_path('logs/paypal.log'),
|
||||
// 'log.LogLevel' => 'FINE'
|
||||
// ));
|
||||
|
||||
// live
|
||||
$this->_apiContext->setConfig(array(
|
||||
'mode' => 'live',
|
||||
'service.EndPoint' => 'https://api.paypal.com',
|
||||
'mode' => $paypal_env,
|
||||
'service.EndPoint' => $paypal_apiUrl,
|
||||
'http.ConnectionTimeOut' => 30,
|
||||
'log.LogEnabled' => true,
|
||||
'log.FileName' => storage_path('logs/paypal.log'),
|
||||
'log.LogLevel' => 'FINE'
|
||||
));
|
||||
|
||||
// live
|
||||
// $this->_apiContext->setConfig(array(
|
||||
// 'mode' => 'sandbox',
|
||||
// 'service.EndPoint' => 'https://api.paypal.com',
|
||||
// 'http.ConnectionTimeOut' => 30,
|
||||
// 'log.LogEnabled' => true,
|
||||
// 'log.FileName' => storage_path('logs/paypal.log'),
|
||||
// 'log.LogLevel' => 'FINE'
|
||||
// ));
|
||||
}
|
||||
|
||||
|
||||
@@ -63,16 +76,54 @@ class PaypalController extends Controller
|
||||
|
||||
public function getCheckout(Request $request)
|
||||
{
|
||||
$UserModel = new UserModel;
|
||||
|
||||
if (Auth::guest()) {
|
||||
|
||||
$message = 'Please <a href="' . url('auth/login') . '">Sign in</a> to your account to proceed.';
|
||||
Session::flash('msg', $message);
|
||||
return Redirect::back();
|
||||
}
|
||||
|
||||
$userId = Auth::user()->id;
|
||||
$array_address_book = $UserModel->selectAddresBook('UserId', $userId);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
$shipping_address_url = "user/address-book/edit/" . $array_address_book[0]->Id;
|
||||
|
||||
if (
|
||||
count($array_address_book) <= 0 ||
|
||||
$array_address_book[0]->Fullname == null ||
|
||||
$array_address_book[0]->ContactNumber == null ||
|
||||
$array_address_book[0]->Address == null ||
|
||||
$array_address_book[0]->State == null ||
|
||||
$array_address_book[0]->City == null ||
|
||||
$array_address_book[0]->ZipCode == null
|
||||
|
||||
) {
|
||||
$message = 'Please complete your shipping address. <a href="' . url($shipping_address_url) . '"> <strong> <u>click here</u> </strong></a>.';
|
||||
Session::flash('cartkeyError', $message);
|
||||
return Redirect::back();
|
||||
}
|
||||
|
||||
// $shippingAddress = [
|
||||
// "recipient_name" => $array_address_book[0]->Fullname,
|
||||
// "line1" => $array_address_book[0]->Address,
|
||||
// "line2" => "",
|
||||
// "city" => $array_address_book[0]->City,
|
||||
// "country_code" => $array_address_book[0]->CountryCode,
|
||||
// "postal_code" => $array_address_book[0]->ZipCode,
|
||||
// "state" => $array_address_book[0]->State,
|
||||
// "phone" => $array_address_book[0]->ContactNumber
|
||||
// ];
|
||||
|
||||
|
||||
// $request->session()->forget('cartkey');
|
||||
if(!$request->session()->has('cartkey')){
|
||||
if (!$request->session()->has('cartkey')) {
|
||||
$message = 'Your cart is empty';
|
||||
Session::flash('cartkeyError', $message);
|
||||
return Redirect::back();
|
||||
@@ -81,81 +132,56 @@ class PaypalController extends Controller
|
||||
$payer = PayPal::Payer();
|
||||
$payer->setPaymentMethod('paypal');
|
||||
|
||||
// $inputFields = Paypal::InputFields();
|
||||
// $inputFields->setAllowNote(true)
|
||||
// ->setNoShipping(1)
|
||||
// ->setAddressOverride(0);
|
||||
// $webProfile = Paypal::WebProfile();
|
||||
// $webProfile->setName("YeowZa! T-Shirt Shop" . uniqid())
|
||||
// // ->setPresentation($presentation)
|
||||
// ->setInputFields($inputFields);
|
||||
|
||||
// $shipping = PayPal::ShippingAddress();
|
||||
// $shipping->setRecipientName($array_address_book[0]->Fullname);
|
||||
// $shipping->setLine1($array_address_book[0]->Address);
|
||||
// $shipping->setCity($array_address_book[0]->City);
|
||||
// $shipping->setCountryCode($array_address_book[0]->CountryCode);
|
||||
// $shipping->setPostalCode($array_address_book[0]->ZipCode);
|
||||
// $shipping->setState($array_address_book[0]->State);
|
||||
// $shipping->setPhone($array_address_book[0]->ContactNumber);
|
||||
|
||||
// $payerInfo = Paypal::PayerInfo();
|
||||
// $payerInfo->setShippingAddress($shipping);
|
||||
// $payer->setPayerInfo($payerInfo);
|
||||
|
||||
$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);
|
||||
|
||||
$getSubtotal = $m->getSubtotal($cartKey);
|
||||
|
||||
$grouped_item = $m->selectTeamStoreGroupByCartKey($cartKey);
|
||||
$store_array = $m->selectTeamStore('Id', $grouped_item[0]->StoreId);
|
||||
|
||||
$getSmallItemQty = 0;
|
||||
$getBulkyItemQty = 0;
|
||||
$getMaskItemQty = 0;
|
||||
$getDGSItemQty = 0;
|
||||
$shippingFee = 0;
|
||||
|
||||
$shippingFee = $this->getShippingFee($cartKey);
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item->VoucherId != null) {
|
||||
$voucherIds[] = $item->VoucherId;
|
||||
$voucher = $m->selectVoucherWhereIn($voucherIds);
|
||||
$item_id = $item->Id;
|
||||
$totalValue = $voucher[0]->VoucherValue;
|
||||
if ($voucher[0]->VoucherType == "Percentage") {
|
||||
$getPercentageValue = $totalValue / 100;
|
||||
$getDiscountValue = ($getSubtotal[0]->Subtotal * $getPercentageValue);
|
||||
$data = array(
|
||||
'Price' => round($getDiscountValue * -1, 2)
|
||||
);
|
||||
$tax = $this->getTax($cartKey)['tax'];
|
||||
$order_grandtotal = $this->getTax($cartKey)['order_grandtotal'];
|
||||
|
||||
$m->updateVoucherValueInCart($data, $item_id);
|
||||
} else {
|
||||
$voucherData = array(
|
||||
'totalValue' => $totalValue,
|
||||
'type' => 'Flat'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if($item->ShippingCostId == 1){
|
||||
$getSmallItemQty += $item->Quantity;
|
||||
}else if($item->ShippingCostId == 2){
|
||||
$getBulkyItemQty += $item->Quantity;
|
||||
}else if($item->ShippingCostId == 3){
|
||||
$getMaskItemQty += $item->Quantity;
|
||||
}else if($item->ShippingCostId == 4){
|
||||
$getDGSItemQty += $item->Quantity;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
$getSmallItemQty = ceil($getSmallItemQty / 3) * 8;
|
||||
$getBulkyItemQty = ceil($getBulkyItemQty / 1) * 8;
|
||||
$getMaskItemQty = ceil($getMaskItemQty / 25) * 8;
|
||||
$getMaskItemQty = ceil($getMaskItemQty / 4) * 5;
|
||||
$shippingFee = $getSmallItemQty + $getBulkyItemQty + $getMaskItemQty + $getDGSItemQty;
|
||||
// var_dump($shippingFee);
|
||||
|
||||
$order_items = array();
|
||||
$updated_items = $m->myCart($cartKey);
|
||||
$updated_getSubtotal = $m->getSubtotal($cartKey);
|
||||
|
||||
// $order_subtotal = $updated_getSubtotal[0]->Subtotal;
|
||||
$order_grandtotal = $updated_getSubtotal[0]->Subtotal;
|
||||
|
||||
if ($grouped_item[0]->StoreId == 76 || $grouped_item[0]->StoreId == 78 || $grouped_item[0]->StoreId == 111 || $grouped_item[0]->StoreId == 131 || $grouped_item[0]->StoreId == 30 || $grouped_item[0]->StoreId == 141 || $grouped_item[0]->StoreId == 162 || $grouped_item[0]->StoreId == 185) {
|
||||
$tax_value = 0;
|
||||
} else {
|
||||
$tax_value = 0.10;
|
||||
}
|
||||
|
||||
$tax = $order_grandtotal * $tax_value;
|
||||
|
||||
foreach ($updated_items as $key => $item) {
|
||||
|
||||
@@ -172,6 +198,29 @@ class PaypalController extends Controller
|
||||
|
||||
$item_list = PayPal::ItemList();
|
||||
$item_list->setItems($order_items);
|
||||
|
||||
$payment = PayPal::Payment();
|
||||
|
||||
// var_dump(array($transaction));
|
||||
if ($array_address_book[0]->CountryCode == "CA") {
|
||||
|
||||
$shipping = PayPal::ShippingAddress();
|
||||
$shipping->setRecipientName($array_address_book[0]->Fullname);
|
||||
$shipping->setLine1($array_address_book[0]->Address);
|
||||
$shipping->setLine2($array_address_book[0]->Address2);
|
||||
$shipping->setCity($array_address_book[0]->City);
|
||||
$shipping->setCountryCode($array_address_book[0]->CountryCode);
|
||||
$shipping->setPostalCode($array_address_book[0]->ZipCode);
|
||||
$shipping->setState($array_address_book[0]->State);
|
||||
$shipping->setPhone($array_address_book[0]->ContactNumber);
|
||||
$item_list->setShippingAddress($shipping);
|
||||
|
||||
$payment->setExperienceProfileId($this->createWebProfile());
|
||||
}
|
||||
|
||||
|
||||
// var_dump($item_list);
|
||||
|
||||
$amount_details = PayPal::Details();
|
||||
$amount_details->setSubtotal($order_grandtotal);
|
||||
$amount_details->setTax($tax);
|
||||
@@ -192,9 +241,10 @@ class PaypalController extends Controller
|
||||
$redirectUrls = PayPal::RedirectUrls();
|
||||
$redirectUrls->setReturnUrl(route('getDone'));
|
||||
$redirectUrls->setCancelUrl(route('getCancel'));
|
||||
// var_dump($shippingAddress)
|
||||
|
||||
|
||||
|
||||
$payment = PayPal::Payment();
|
||||
$payment->setIntent('sale');
|
||||
$payment->setPayer($payer);
|
||||
$payment->setRedirectUrls($redirectUrls);
|
||||
@@ -233,19 +283,306 @@ class PaypalController extends Controller
|
||||
|
||||
}
|
||||
|
||||
public function getShippingFee($cartKey)
|
||||
{
|
||||
|
||||
$m = new TeamStoreModel;
|
||||
$UserModel = new UserModel;
|
||||
$userId = 0;
|
||||
if (!Auth::guest()) {
|
||||
$userId = Auth::user()->id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$items = $m->myCart($cartKey); // item from cart_tmp
|
||||
$getSubtotal = $m->getSubtotal($cartKey);
|
||||
$getSmallItemQty = 0;
|
||||
$getBulkyItemQty = 0;
|
||||
$getMaskItemQty = 0;
|
||||
$getDGSItemQty = 0;
|
||||
$shippingFee = 0;
|
||||
$CAShippingfee = 0;
|
||||
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item->VoucherId != null) {
|
||||
$voucherIds[] = $item->VoucherId;
|
||||
$voucher = $m->selectVoucherWhereIn($voucherIds);
|
||||
$item_id = $item->Id;
|
||||
$totalValue = $voucher[0]->VoucherValue;
|
||||
if ($voucher[0]->VoucherType == "Percentage") {
|
||||
$getPercentageValue = $totalValue / 100;
|
||||
$getDiscountValue = ($getSubtotal[0]->Subtotal * $getPercentageValue);
|
||||
$data = array(
|
||||
'Price' => round($getDiscountValue * -1, 2)
|
||||
);
|
||||
|
||||
$m->updateVoucherValueInCart($data, $item_id);
|
||||
} else {
|
||||
$voucherData = array(
|
||||
'totalValue' => $totalValue,
|
||||
'type' => 'Flat'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($item->ShippingCostId == 1) {
|
||||
$getSmallItemQty += $item->Quantity;
|
||||
} else if ($item->ShippingCostId == 2) {
|
||||
$getBulkyItemQty += $item->Quantity;
|
||||
} else if ($item->ShippingCostId == 3) {
|
||||
$getMaskItemQty += $item->Quantity;
|
||||
} else if ($item->ShippingCostId == 4) {
|
||||
$getDGSItemQty += $item->Quantity;
|
||||
}
|
||||
|
||||
$CAShippingfee += $item->Quantity; // for canada
|
||||
}
|
||||
|
||||
$array_address_book = $UserModel->selectAddresBook('UserId', $userId);
|
||||
$countryCode = "";
|
||||
if (count($array_address_book) > 0) {
|
||||
$countryCode = $array_address_book[0]->CountryCode;
|
||||
$CAShippingfee = ceil($CAShippingfee / 2) * 30;
|
||||
}
|
||||
|
||||
$getSmallItemQty = ceil($getSmallItemQty / 3) * 8;
|
||||
$getBulkyItemQty = ceil($getBulkyItemQty / 1) * 8;
|
||||
$getMaskItemQty = ceil($getMaskItemQty / 25) * 8;
|
||||
$getDGSItemQty = ceil($getDGSItemQty / 4) * 5;
|
||||
$shippingFee = $getSmallItemQty + $getBulkyItemQty + $getMaskItemQty + $getDGSItemQty;
|
||||
|
||||
if ($countryCode == "CA") {
|
||||
$shippingFee = $CAShippingfee;
|
||||
}
|
||||
|
||||
return $shippingFee;
|
||||
}
|
||||
|
||||
public function getTax($cartKey)
|
||||
{
|
||||
$m = new TeamStoreModel;
|
||||
$updated_getSubtotal = $m->getSubtotal($cartKey);
|
||||
$grouped_item = $m->selectTeamStoreGroupByCartKey($cartKey);
|
||||
|
||||
if (count($grouped_item) <= 0) {
|
||||
$tax_value = 0;
|
||||
} else {
|
||||
if ($grouped_item[0]->StoreId == 76 || $grouped_item[0]->StoreId == 78 || $grouped_item[0]->StoreId == 111 || $grouped_item[0]->StoreId == 131 || $grouped_item[0]->StoreId == 30 || $grouped_item[0]->StoreId == 141 || $grouped_item[0]->StoreId == 162 || $grouped_item[0]->StoreId == 185 || $grouped_item[0]->StoreId == 244) {
|
||||
$tax_value = 0;
|
||||
} else {
|
||||
$tax_value = 0.10;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$order_grandtotal = $updated_getSubtotal[0]->Subtotal;
|
||||
|
||||
|
||||
|
||||
$tax = $order_grandtotal * $tax_value;
|
||||
|
||||
return [
|
||||
'tax' => $tax,
|
||||
'order_grandtotal' => $order_grandtotal,
|
||||
];
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
try {
|
||||
$payment = PayPal::getById($id, $this->_apiContext);
|
||||
|
||||
$payment = PayPal::getById($id, $this->_apiContext);
|
||||
$paymentExecution = PayPal::PaymentExecution();
|
||||
|
||||
$paymentExecution = PayPal::PaymentExecution();
|
||||
$paymentExecution->setPayerId($payer_id);
|
||||
$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(", ", array_filter($other_email, function ($value) {
|
||||
return !is_null($value) && $value !== '';
|
||||
}));
|
||||
$email_cc = "orders@crewsportswear.com" . "," . $other_email;
|
||||
} else {
|
||||
$email_cc = "orders@crewsportswear.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('no-reply@crewsportswear.com', 'CREW Sportswear');
|
||||
$message->bcc($data['email_cc'], 'Orders From CREW Sportswear');
|
||||
$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();
|
||||
echo $e->getData();
|
||||
Session::put('cartkeyError', 'Invalid payment.');
|
||||
return Redirect::route('cart');
|
||||
}
|
||||
|
||||
$paymentExecution->setPayerId($payer_id);
|
||||
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
|
||||
|
||||
// print_r($executePayment);
|
||||
// if ($executePayment->getState() == 'approved') {
|
||||
@@ -257,157 +594,9 @@ class PaypalController extends Controller
|
||||
// // return Redirect::route('paywithpaypal');
|
||||
// echo 'Payment success';
|
||||
// }
|
||||
$obj = json_decode($executePayment);
|
||||
|
||||
|
||||
$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;
|
||||
|
||||
//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;
|
||||
|
||||
$items = $m->myCart($cartKey); // item from cart_tmp
|
||||
$getSubtotal = $m->getSubtotal($cartKey);
|
||||
|
||||
$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(", ", array_filter($other_email, function($value) { return !is_null($value) && $value !== ''; }));
|
||||
$email_cc = "orders@crewsportswear.com" . "," . $other_email;
|
||||
} else {
|
||||
$email_cc = "orders@crewsportswear.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('no-reply@crewsportswear.com', 'CREW Sportswear');
|
||||
$message->bcc($data['email_cc'], 'Orders From CREW Sportswear');
|
||||
$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);
|
||||
// var_dump($obj->payer->payer_info->shipping_address);
|
||||
// var_dump($obj->transactions[0]->item_list->shipping_address);
|
||||
}
|
||||
|
||||
|
||||
@@ -419,6 +608,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;
|
||||
@@ -441,4 +634,47 @@ class PaypalController extends Controller
|
||||
|
||||
return redirect()->route('cart');
|
||||
}
|
||||
}
|
||||
|
||||
public function createWebProfile()
|
||||
{
|
||||
$UserModel = new UserModel;
|
||||
$userId = Auth::user()->id;
|
||||
$array_address_book = $UserModel->selectAddresBook('UserId', $userId);
|
||||
if (count($array_address_book) <= 0) {
|
||||
$message = 'Please add Shipping address.';
|
||||
Session::flash('cartkeyError', $message);
|
||||
return Redirect::back();
|
||||
}
|
||||
|
||||
// $shippingAddress = [
|
||||
// "recipient_name" => ,
|
||||
|
||||
$flowConfig = PayPal::FlowConfig();
|
||||
$presentation = PayPal::Presentation();
|
||||
$inputFields = PayPal::InputFields();
|
||||
$webProfile = PayPal::WebProfile();
|
||||
|
||||
|
||||
// $presentation->setLogoImage("https://www.crewsportswear.com/beta/public/images/logo.png")->setBrandName("Merchbay"); //NB: Paypal recommended to use https for the logo's address and the size set to 190x60.
|
||||
$presentation->setBrandName("Crewsportswear"); //NB: Paypal recommended to use https for the logo's address and the size set to 190x60.
|
||||
|
||||
if ($array_address_book[0]->CountryCode == "CA") {
|
||||
$flowConfig->setLandingPageType("Billing"); //Set the page type
|
||||
$inputFields->setAllowNote(true)->setAddressOverride(1);
|
||||
} else {
|
||||
$inputFields->setAllowNote(true)->setAddressOverride(0);
|
||||
}
|
||||
|
||||
|
||||
$webProfile->setName("Crewsportswear " . uniqid())
|
||||
->setFlowConfig($flowConfig)
|
||||
// Parameters for style and presentation.
|
||||
->setPresentation($presentation)
|
||||
// Parameters for input field customization.
|
||||
->setInputFields($inputFields);
|
||||
|
||||
$createProfileResponse = $webProfile->create($this->_apiContext);
|
||||
|
||||
return $createProfileResponse->getId(); //The new webprofile's id
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user