Commit f68b67a4 by Angel MAS

medio acordar con el vendedor

parent 3e82a44a
...@@ -16,6 +16,7 @@ use Onestartup\Shop\Model\DetailShop as Item; ...@@ -16,6 +16,7 @@ use Onestartup\Shop\Model\DetailShop as Item;
use Onestartup\Shop\Payment\MP; use Onestartup\Shop\Payment\MP;
use Onestartup\Shop\Notifications\PaymentClient; use Onestartup\Shop\Notifications\PaymentClient;
use Onestartup\Shop\Notifications\PaymentAcordarClient;
use Onestartup\Shop\Notifications\PaymentClientCard; use Onestartup\Shop\Notifications\PaymentClientCard;
use PayPal\Rest\ApiContext; use PayPal\Rest\ApiContext;
...@@ -42,99 +43,97 @@ class CartController extends Controller ...@@ -42,99 +43,97 @@ class CartController extends Controller
public function show() public function show()
{ {
$total = $this->total(); $total = $this->total();
$cart = \Session::get('cart'); $cart = \Session::get('cart');
return view('shop-public::cart.show-cart') return view('shop-public::cart.show-cart')
->with('cart', $cart) ->with('cart', $cart)
->with('total', $total); ->with('total', $total);
} }
public function add(Product $product) public function add(Product $product)
{ {
if (\Session::has('sale')) { if (\Session::has('sale')) {
$sale = Sale::find(\Session::get('sale')->id); $sale = Sale::find(\Session::get('sale')->id);
$sale->status = 0; $sale->status = 0;
$sale->save(); $sale->save();
\Session::forget('sale'); \Session::forget('sale');
} }
$cart = \Session::get('cart'); $cart = \Session::get('cart');
$quantity = 1; $quantity = 1;
if (isset($cart[$product->slug])) {
$quantity = $cart[$product->slug]->quantity + 1;
}
$product->quantity = $quantity; if (isset($cart[$product->slug])) {
$cart[$product->slug] = $product; $quantity = $cart[$product->slug]->quantity + 1;
}
\Session::put('cart', $cart); $product->quantity = $quantity;
$cart[$product->slug] = $product;
return redirect()->route('cart.show'); \Session::put('cart', $cart);
}
return redirect()->route('cart.show');
}
public function remove(Product $product)
{
$cart = \Session::get('cart');
if (!isset($cart[$product->slug])) { public function remove(Product $product)
return redirect()->route('cart.show'); {
} $cart = \Session::get('cart');
unset($cart[$product->slug]); if (!isset($cart[$product->slug])) {
\Session::put('cart', $cart);
return redirect()->route('cart.show');
}
public function update(Product $product, $quantity)
{
if (\Session::has('sale')) {
$sale = Sale::find(\Session::get('sale')->id);
$sale->status = 0;
$sale->save();
\Session::forget('sale');
}
if ($quantity <= 0) {
return redirect()->back();
}
$cart = \Session::get('cart');
if (!isset($cart[$product->slug])) {
return redirect()->route('cart.show');
}
$cart[$product->slug]->quantity = $quantity;
\Session::put('cart', $cart);
return redirect()->route('cart.show'); return redirect()->route('cart.show');
} }
public function trash() unset($cart[$product->slug]);
{ \Session::put('cart', $cart);
return redirect()->route('cart.show');
if (\Session::has('sale')) { }
$sale = Sale::find(\Session::get('sale')->id);
$sale->status = 0;
$sale->save();
}
\Session::forget('cart'); public function update(Product $product, $quantity)
{
if (\Session::has('sale')) {
$sale = Sale::find(\Session::get('sale')->id);
$sale->status = 0;
$sale->save();
\Session::forget('sale'); \Session::forget('sale');
\Session::forget('client'); }
if ($quantity <= 0) {
return redirect()->back();
}
$cart = \Session::get('cart');
if (!isset($cart[$product->slug])) {
return redirect()->route('cart.show'); return redirect()->route('cart.show');
} }
$cart[$product->slug]->quantity = $quantity;
\Session::put('cart', $cart);
return redirect()->route('cart.show');
}
public function shipping() public function trash()
{ {
/*\Session::forget('client');
\Session::forget('cart');
\Session::forget('sale');*/
$client = new Client(); if (\Session::has('sale')) {
$addres = new Addres(); $sale = Sale::find(\Session::get('sale')->id);
$sale->status = 0;
$sale->save();
}
\Session::forget('cart');
\Session::forget('sale');
\Session::forget('client');
if(count(\Session::get('cart')) <= 0) {return redirect()->route('cart.show');} return redirect()->route('cart.show');
}
public function shipping()
{
$client = new Client();
$addres = new Addres();
if(count(\Session::get('cart')) <= 0) {return redirect()->route('cart.show');}
if(\Session::has('client')) { if(\Session::has('client')) {
$client = \Session::get('client'); $client = \Session::get('client');
$addres = $client->shipping; $addres = $client->shipping;
...@@ -144,15 +143,15 @@ class CartController extends Controller ...@@ -144,15 +143,15 @@ class CartController extends Controller
$total = $this->total(); $total = $this->total();
$shipping = Shipping::select( $shipping = Shipping::select(
\DB::raw("CONCAT(name,' $',cost) AS name"),'id') \DB::raw("CONCAT(name,' $',cost) AS name"),'id')
->pluck('name', 'id'); ->pluck('name', 'id');
return view('shop-public::cart.shipping') return view('shop-public::cart.shipping')
->with('cart', $cart) ->with('cart', $cart)
->with('total', $total) ->with('total', $total)
->with('client', $client) ->with('client', $client)
->with('addres', $addres) ->with('addres', $addres)
->with('shipping', $shipping); ->with('shipping', $shipping);
} }
public function storeClient(Request $request) public function storeClient(Request $request)
...@@ -168,7 +167,7 @@ class CartController extends Controller ...@@ -168,7 +167,7 @@ class CartController extends Controller
$client = new Client(); $client = new Client();
$addres = new Addres(); $addres = new Addres();
} }
} }
/** Crea cliente ········· */ /** Crea cliente ········· */
...@@ -181,14 +180,14 @@ class CartController extends Controller ...@@ -181,14 +180,14 @@ class CartController extends Controller
if (!isset($addres->client_id)) { if (!isset($addres->client_id)) {
$addres->client_id = $client->id; $addres->client_id = $client->id;
} }
$addres->save(); $addres->save();
/** Guarda productos en tabla ······ */ /** Guarda productos en tabla ······ */
$costoEnvio = $addres->shipping_price->cost; $costoEnvio = $addres->shipping_price->cost;
if (!\Session::has('sale')) { if (!\Session::has('sale')) {
$sale = $client->sales()->save(new Sale(['status' => 1, 'total'=>($this->total() + $costoEnvio)])); $sale = $client->sales()->save(new Sale(['status' => 1, 'total'=>($this->total() + $costoEnvio)]));
foreach ($cart as $product) { foreach ($cart as $product) {
...@@ -199,7 +198,7 @@ class CartController extends Controller ...@@ -199,7 +198,7 @@ class CartController extends Controller
]); ]);
} }
} else { } else {
$sale = Sale::find(\Session::get('sale')->id); $sale = Sale::find(\Session::get('sale')->id);
$sale->total = ($this->total() + $costoEnvio); $sale->total = ($this->total() + $costoEnvio);
...@@ -226,13 +225,13 @@ class CartController extends Controller ...@@ -226,13 +225,13 @@ class CartController extends Controller
$client = $sale->client; $client = $sale->client;
$items = $sale->items; $items = $sale->items;
$addres = $client->shipping; $addres = $client->shipping;
return view('shop-public::cart.finish') return view('shop-public::cart.finish')
->with('sale', $sale) ->with('sale', $sale)
->with('client', $client) ->with('client', $client)
->with('items', $items) ->with('items', $items)
->with('total', $this->total()) ->with('total', $this->total())
->with('addres', $addres); ->with('addres', $addres);
} }
public function cancel() public function cancel()
...@@ -244,7 +243,7 @@ class CartController extends Controller ...@@ -244,7 +243,7 @@ class CartController extends Controller
\Session::forget('sale'); \Session::forget('sale');
return redirect()->route('cart.show'); return redirect()->route('cart.show');
} }
public function payment(Request $request) public function payment(Request $request)
...@@ -303,8 +302,8 @@ class CartController extends Controller ...@@ -303,8 +302,8 @@ class CartController extends Controller
$user->notify(new PaymentClientCard($client, $request->paymentMethodId, $status)); $user->notify(new PaymentClientCard($client, $request->paymentMethodId, $status));
return redirect() return redirect()
->route('main.shop') ->route('main.shop')
->with('payment_approved', 'payment_approved'); ->with('payment_approved', 'payment_approved');
} elseif ($payment['response']['status'] == 'in_process') { } elseif ($payment['response']['status'] == 'in_process') {
...@@ -320,24 +319,24 @@ class CartController extends Controller ...@@ -320,24 +319,24 @@ class CartController extends Controller
$user->notify(new PaymentClientCard($client, $request->paymentMethodId, $status)); $user->notify(new PaymentClientCard($client, $request->paymentMethodId, $status));
return redirect() return redirect()
->route('main.shop') ->route('main.shop')
->with('payment_pending', 'payment_pending'); ->with('payment_pending', 'payment_pending');
} else { } else {
$status = 'Rechazado'; $status = 'Rechazado';
$user->notify(new PaymentClientCard($client, $request->paymentMethodId, $status)); $user->notify(new PaymentClientCard($client, $request->paymentMethodId, $status));
return redirect() return redirect()
->back() ->back()
->with('payment_reject', 'payment_reject'); ->with('payment_reject', 'payment_reject');
} }
//dd($payment); //dd($payment);
...@@ -373,9 +372,9 @@ class CartController extends Controller ...@@ -373,9 +372,9 @@ class CartController extends Controller
$payment_data = array( $payment_data = array(
"transaction_amount" => $this->total() + $addres->shipping_price->cost, "transaction_amount" => $this->total() + $addres->shipping_price->cost,
"description" => $detail, "description" => $detail,
"payment_method_id" => $request->payment_id, "payment_method_id" => $request->payment_id,
"payer" => array ( "payer" => array (
"email" => $client->email, "email" => $client->email,
...@@ -401,11 +400,11 @@ class CartController extends Controller ...@@ -401,11 +400,11 @@ class CartController extends Controller
return redirect() return redirect()
->route('main.shop') ->route('main.shop')
->with('url_ficha', $url_ficha); ->with('url_ficha', $url_ficha);
} }
...@@ -434,17 +433,17 @@ class CartController extends Controller ...@@ -434,17 +433,17 @@ class CartController extends Controller
new OAuthTokenCredential( new OAuthTokenCredential(
env('CLIENT_ID_PAYPAL'), env('CLIENT_ID_PAYPAL'),
env('SECRET_PAYPAL') env('SECRET_PAYPAL')
) )
); );
$apiContext->setConfig( $apiContext->setConfig(
array( array(
'log.LogEnabled' => true, 'log.LogEnabled' => true,
'log.FileName' => base_path().'/Paypal.log', 'log.FileName' => base_path().'/Paypal.log',
'log.LogLevel' => 'DEBUG', 'log.LogLevel' => 'DEBUG',
'mode' => env('PAYPAL_MODE'), 'mode' => env('PAYPAL_MODE'),
) )
); );
$paymentId = $request->paymentId; $paymentId = $request->paymentId;
...@@ -457,7 +456,7 @@ class CartController extends Controller ...@@ -457,7 +456,7 @@ class CartController extends Controller
$result = $payment->execute($execution, $apiContext); $result = $payment->execute($execution, $apiContext);
if ($result->state == 'approved') { if ($result->state == 'approved') {
$sale = Sale::find(\Session::get('sale')->id); $sale = Sale::find(\Session::get('sale')->id);
$client = $sale->client; $client = $sale->client;
$items = $sale->items; $items = $sale->items;
...@@ -475,12 +474,12 @@ class CartController extends Controller ...@@ -475,12 +474,12 @@ class CartController extends Controller
\Session::forget('sale'); \Session::forget('sale');
return redirect() return redirect()
->route('main.shop') ->route('main.shop')
->with('payment_approved', 'payment_approved'); ->with('payment_approved', 'payment_approved');
} else { } else {
return redirect() return redirect()
->route('cart.finish') ->route('cart.finish')
->with('paypal_fail',$result->state); ->with('paypal_fail',$result->state);
} }
} catch (\PayPal\Exception\PayPalConnectionException $ex) { } catch (\PayPal\Exception\PayPalConnectionException $ex) {
...@@ -494,8 +493,8 @@ class CartController extends Controller ...@@ -494,8 +493,8 @@ class CartController extends Controller
public function cancelPaypal() public function cancelPaypal()
{ {
return redirect() return redirect()
->route('cart.finish') ->route('cart.finish')
->with('paypal_cancel', 'paypal_cancel'); ->with('paypal_cancel', 'paypal_cancel');
} }
public function paymentPaypal(Request $request) public function paymentPaypal(Request $request)
...@@ -515,25 +514,25 @@ class CartController extends Controller ...@@ -515,25 +514,25 @@ class CartController extends Controller
$addres = $client->shipping; $addres = $client->shipping;
$detail = ''; $detail = '';
$apiContext = new ApiContext( $apiContext = new ApiContext(
new OAuthTokenCredential( new OAuthTokenCredential(
env('CLIENT_ID_PAYPAL'), env('CLIENT_ID_PAYPAL'),
env('SECRET_PAYPAL') env('SECRET_PAYPAL')
) )
); );
$apiContext->setConfig( $apiContext->setConfig(
array( array(
'log.LogEnabled' => true, 'log.LogEnabled' => true,
'log.FileName' => base_path().'/Paypal.log', 'log.FileName' => base_path().'/Paypal.log',
'log.LogLevel' => 'DEBUG', 'log.LogLevel' => 'DEBUG',
'mode' => env('PAYPAL_MODE'), 'mode' => env('PAYPAL_MODE'),
) )
); );
$payer = new \PayPal\Api\Payer(); $payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal'); $payer->setPaymentMethod('paypal');
...@@ -542,26 +541,26 @@ class CartController extends Controller ...@@ -542,26 +541,26 @@ class CartController extends Controller
foreach ($sale->items as $item) { foreach ($sale->items as $item) {
//$cost = $item->product->infoSale->sale_price * $item->quantity; //$cost = $item->product->infoSale->sale_price * $item->quantity;
$item1 = new PaypalItem(); $item1 = new PaypalItem();
$item1->setName($item->product->name) $item1->setName($item->product->name)
->setDescription($item->product->name) ->setDescription($item->product->name)
->setCurrency('MXN') ->setCurrency('MXN')
->setQuantity($item->quantity) ->setQuantity($item->quantity)
->setPrice($item->product->infoSale->sale_price); ->setPrice($item->product->infoSale->sale_price);
$items[]=$item1; $items[]=$item1;
} }
$item2 = new PaypalItem(); $item2 = new PaypalItem();
$item2->setName($addres->shipping_price->name) $item2->setName($addres->shipping_price->name)
->setDescription($addres->shipping_price->name) ->setDescription($addres->shipping_price->name)
->setCurrency('MXN') ->setCurrency('MXN')
->setQuantity(1) ->setQuantity(1)
->setPrice($addres->shipping_price->cost); ->setPrice($addres->shipping_price->cost);
array_push($items, $item2); array_push($items, $item2);
$itemList = new ItemList(); $itemList = new ItemList();
$itemList->setItems($items); $itemList->setItems($items);
...@@ -569,44 +568,79 @@ class CartController extends Controller ...@@ -569,44 +568,79 @@ class CartController extends Controller
$details = new Details(); $details = new Details();
$details->setSubtotal($this->total() + $addres->shipping_price->cost); $details->setSubtotal($this->total() + $addres->shipping_price->cost);
$amount = new \PayPal\Api\Amount(); $amount = new \PayPal\Api\Amount();
$amount->setCurrency("MXN") $amount->setCurrency("MXN")
->setTotal($this->total() + $addres->shipping_price->cost) ->setTotal($this->total() + $addres->shipping_price->cost)
->setDetails($details); ->setDetails($details);
$transaction = new \PayPal\Api\Transaction(); $transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount) $transaction->setAmount($amount)
->setItemList($itemList) ->setItemList($itemList)
->setDescription("Compra en Onestartup") ->setDescription("Compra en Onestartup")
->setInvoiceNumber(uniqid()); ->setInvoiceNumber(uniqid());
$redirectUrls = new \PayPal\Api\RedirectUrls(); $redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl(route('cart.successPaypal')) $redirectUrls->setReturnUrl(route('cart.successPaypal'))
->setCancelUrl(route('cart.cancelPaypal')); ->setCancelUrl(route('cart.cancelPaypal'));
$payment = new \PayPal\Api\Payment(); $payment = new \PayPal\Api\Payment();
$payment->setIntent('sale') $payment->setIntent('sale')
->setPayer($payer) ->setPayer($payer)
->setRedirectUrls($redirectUrls) ->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction)); ->setTransactions(array($transaction));
try { try {
$payment->create($apiContext); $payment->create($apiContext);
$sale->status = 3; $sale->status = 3;
$sale->transaction_id = $payment->id; $sale->transaction_id = $payment->id;
$sale->save(); $sale->save();
return redirect($payment->getApprovalLink()); return redirect($payment->getApprovalLink());
} }
catch (\PayPal\Exception\PayPalConnectionException $ex) { catch (\PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getData(); echo $ex->getData();
} }
} }
public function acordar(Request $request)
{
if (!\Session::has('client')) {
return redirect()->route('cart.shipping');
}
if (!\Session::has('sale')) {
return redirect()->route('cart.shipping');
}
$sale = Sale::find(\Session::get('sale')->id);
$client = $sale->client;
$items = $sale->items;
$addres = $client->shipping;
$detail = '';
$sale->status = 3;
$sale->transaction_id = null;
$sale->payment_type = 'Acordar con vendedor';
$sale->save();
\Session::forget('client');
\Session::forget('cart');
\Session::forget('sale');
$user = \App\User::first();
$user->notify(new PaymentAcordarClient('Acordar con el vendedor', $client));
return redirect()
->route('main.shop')
->with('acordar', 'acordar');
}
} }
<?php
namespace Onestartup\Shop\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Onestartup\Shop\Model\ClientShop as Client;
class PaymentAcordarClient extends Notification
{
use Queueable;
protected $tipo;
protected $client;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($tipo, Client $client)
{
$this->tipo = $tipo;
$this->client = $client;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('¡Hola!')
->subject('Orden: Acordar con el vendedor '.env('APP_NAME'))
//->replyTo($this->interested->email, $this->interested->name)
->line('Un cliente elijió la opcion de acordar con el vendedor')
->line('Datos del cliente:')
->line('Nombre: '.$this->client->name.' '.$this->client->lastname)
->line('Telefono: '.$this->client->phone)
->line('Correo: '.$this->client->email);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
...@@ -86,6 +86,7 @@ Route::group(['middleware' => ['web']], function(){ ...@@ -86,6 +86,7 @@ Route::group(['middleware' => ['web']], function(){
Route::post('cart/payment', 'Onestartup\Shop\Controller\CartController@payment')->name('cart.payment'); Route::post('cart/payment', 'Onestartup\Shop\Controller\CartController@payment')->name('cart.payment');
Route::post('cart/paymentCash', 'Onestartup\Shop\Controller\CartController@paymentCash')->name('cart.paymentCash'); Route::post('cart/paymentCash', 'Onestartup\Shop\Controller\CartController@paymentCash')->name('cart.paymentCash');
Route::post('cart/acordar', 'Onestartup\Shop\Controller\CartController@acordar')->name('cart.acordar');
Route::get('cart/update/{product_slug}/{quantity}', 'Onestartup\Shop\Controller\CartController@update') Route::get('cart/update/{product_slug}/{quantity}', 'Onestartup\Shop\Controller\CartController@update')
->where('quantity', '[0-9]+') ->where('quantity', '[0-9]+')
......
...@@ -67,6 +67,10 @@ setlocale(LC_MONETARY, 'en_US'); ...@@ -67,6 +67,10 @@ setlocale(LC_MONETARY, 'en_US');
@include('shop-public::forms.form-paypal') @include('shop-public::forms.form-paypal')
<br> <br>
<br>
@include('shop-public::forms.form-acordar')
<br>
</section> </section>
......
<form action="{{route('cart.acordar')}}" method="POST">
{{ csrf_field() }}
<button type="submit">
Acordar con el vendedor: {{ money_format('%(#10n', ($total + $addres->shipping_price->cost)) }}
</button>
</form>
\ No newline at end of file
...@@ -64,6 +64,12 @@ $util = new Onestartup\Shop\Libs\Util(); ...@@ -64,6 +64,12 @@ $util = new Onestartup\Shop\Libs\Util();
</script> </script>
@endif @endif
@if(Session::has('acordar'))
<script type="text/javascript">
swal("Compra registrada", "Gracias! En breve nos comunicaremos contigo para cerrar la venta", "success")
</script>
@endif
@if(Session::has('url_ficha')) @if(Session::has('url_ficha'))
<script> <script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment