257 lines
6.6 KiB
PHP
257 lines
6.6 KiB
PHP
<?php namespace App\Http\Controllers\paypal;
|
|
|
|
use App\Http\Requests;
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Paypal;
|
|
use App\Models\teamstore\TeamStoreModel;
|
|
use App\Models\paypal\PayPalModel;
|
|
use Auth;
|
|
use Session;
|
|
use Redirect;
|
|
|
|
|
|
class PaypalController extends Controller {
|
|
|
|
private $_apiContext;
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->_apiContext = PayPal::ApiContext(
|
|
config('services.paypal.client_id'),
|
|
config('services.paypal.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',
|
|
// 'http.ConnectionTimeOut' => 30,
|
|
// 'log.LogEnabled' => true,
|
|
// 'log.FileName' => storage_path('logs/paypal.log'),
|
|
// 'log.LogLevel' => 'FINE'
|
|
// ));
|
|
}
|
|
|
|
|
|
public function payPremium()
|
|
{
|
|
return view('payPremium');
|
|
}
|
|
|
|
|
|
public function getCheckout(Request $request)
|
|
{
|
|
|
|
if(Auth::guest()){
|
|
|
|
Session::flash('msg', 'Please login to proceed.');
|
|
return Redirect::back();
|
|
}
|
|
|
|
$payer = PayPal::Payer();
|
|
$payer->setPaymentMethod('paypal');
|
|
|
|
|
|
|
|
$m = new TeamStoreModel;
|
|
$cartKey = $request->session()->get('cartkey');
|
|
|
|
$items = $m->myCart($cartKey);
|
|
$getSubtotal = $m->getSubtotal($cartKey);
|
|
|
|
// var_dump($getSubtotal[0]->Subtotal);
|
|
|
|
$order_subtotal = $getSubtotal[0]->Subtotal;
|
|
$order_grandtotal = $getSubtotal[0]->Subtotal;
|
|
|
|
$order_items = array();
|
|
|
|
foreach($items as $key => $item){
|
|
if($item->Order == "Jersey"){
|
|
|
|
$itemOrder = " (Jersey Only)";
|
|
|
|
}elseif ($item->Order == "Shorts"){
|
|
|
|
$itemOrder = " (Shorts Only)";
|
|
|
|
}else{
|
|
$itemOrder = " (w/ Shorts)";
|
|
}
|
|
|
|
// $descriptions = "Name: " . $item->Name . " Number: " . $item->Number . " Size: " . $item->Size;"?"
|
|
|
|
$order_items[$key] = PayPal::Item();
|
|
$order_items[$key]->setName($item->ProductName);
|
|
$order_items[$key]->setCurrency('USD');
|
|
$order_items[$key]->setQuantity($item->Quantity);
|
|
// $order_items[$key]->setDescription($descriptions);
|
|
$order_items[$key]->setPrice($item->Price);
|
|
}
|
|
|
|
$item_list = PayPal::ItemList();
|
|
$item_list->setItems($order_items);
|
|
|
|
$amount_details = PayPal::Details();
|
|
$amount_details->setSubtotal($order_subtotal);
|
|
|
|
$amount = PayPal::Amount();
|
|
$amount->setCurrency('USD');
|
|
$amount->setDetails($amount_details);
|
|
$amount->setTotal($order_grandtotal);
|
|
|
|
$transaction = PayPal::Transaction();
|
|
$transaction->setAmount($amount);
|
|
$transaction->setItemList($item_list);
|
|
$transaction->setDescription('Your transaction description');
|
|
$transaction->setInvoiceNumber(date('Y') . '-' . uniqid());
|
|
|
|
$redirectUrls = PayPal:: RedirectUrls();
|
|
$redirectUrls->setReturnUrl(route('getDone'));
|
|
$redirectUrls->setCancelUrl(route('getCancel'));
|
|
|
|
|
|
$payment = PayPal::Payment();
|
|
$payment->setIntent('sale');
|
|
$payment->setPayer($payer);
|
|
$payment->setRedirectUrls($redirectUrls);
|
|
$payment->setTransactions(array($transaction));
|
|
|
|
|
|
$response = $payment->create($this->_apiContext);
|
|
$redirectUrl = $response->links[1]->href;
|
|
|
|
|
|
return redirect()->to( $redirectUrl );
|
|
}
|
|
|
|
public function getDoneTest()
|
|
{
|
|
// $paymentId= "PAY-66Y799521H279203PLOP2X4Y";
|
|
// $payment = PayPal::getById($paymentId, $this->_apiContext);
|
|
|
|
// $obj = json_decode($payment);
|
|
// // var_dump($obj);
|
|
|
|
// $total = $obj->transactions[0]->amount->total;
|
|
// $currency = $obj->transactions[0]->amount->currency;
|
|
// $invoice_number = $obj->transactions[0]->invoice_number;
|
|
|
|
// return view('paypal.get_done')
|
|
// ->with('currency', $currency)
|
|
// ->with('total', $total);
|
|
// try {
|
|
// $invoice = PayPal::Invoice();
|
|
// echo $number = $invoice->generateNumber($this->_apiContext);
|
|
// } catch (Exception $ex) {
|
|
// echo $ex;
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
public function getDone(Request $request)
|
|
{
|
|
$id = $request->get('paymentId');
|
|
$token = $request->get('token');
|
|
$payer_id = $request->get('PayerID');
|
|
|
|
|
|
$payment = PayPal::getById($id, $this->_apiContext);
|
|
|
|
$paymentExecution = PayPal::PaymentExecution();
|
|
|
|
$paymentExecution->setPayerId($payer_id);
|
|
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
|
|
|
|
// 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';
|
|
// }
|
|
$obj = json_decode($executePayment);
|
|
|
|
$total = $obj->transactions[0]->amount->total;
|
|
$currency = $obj->transactions[0]->amount->currency;
|
|
$invoice_number = $obj->transactions[0]->invoice_number;
|
|
|
|
// var_dump($obj->transactions[0]->amount);
|
|
/// end paypal codes
|
|
|
|
$paypal_model = new PayPalModel;
|
|
$m = new TeamStoreModel;
|
|
$cartKey = $request->session()->get('cartkey');
|
|
$userId = Auth::user()->id;
|
|
|
|
$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
|
|
);
|
|
|
|
$i = $paypal_model->insertToPaypalDetails($payment_details);
|
|
|
|
// var_dump($i);
|
|
|
|
// foreach($items as $key => $val){
|
|
// $orders[] = array(
|
|
// 'Order' => $val->Order,
|
|
// 'ProductId' => $val->ProductId,
|
|
// 'CartKey' => $val->CartKey,
|
|
// 'ProductURL' => $val->ProductURL,
|
|
// 'TeamName' => $val->TeamName,
|
|
// 'Name' => $val->Name,
|
|
// 'Number' => $val->Number,
|
|
// 'Size' => $val->Size,
|
|
// 'Price' => $val->Price,
|
|
// 'Quantity' => $val->Quantity,
|
|
// );
|
|
// }
|
|
|
|
|
|
var_dump($items);
|
|
|
|
// $paypal_model->insertToOrders($items); // insert to orders table
|
|
|
|
|
|
// $request->session()->forget('cartkey'); // clear session for cartkey
|
|
|
|
// return view('paypal.get_done')
|
|
// ->with('currency', $currency)
|
|
// ->with('total', $total);
|
|
|
|
}
|
|
|
|
|
|
public function getCancel()
|
|
{
|
|
return redirect()->route('cart');
|
|
}
|
|
|
|
}
|