Commit d3a2de64 by Angel MAS

open pay integration

parent 535db319
...@@ -37,6 +37,7 @@ use PayPal\Api\ItemList; ...@@ -37,6 +37,7 @@ use PayPal\Api\ItemList;
use PayPal\Api\Details; use PayPal\Api\Details;
use Onestartup\Shop\Libs\Util; use Onestartup\Shop\Libs\Util;
use Onestartup\Shop\Libs\PaymentOpenPay;
use Onestartup\Shop\Model\VariableExtra as Variable; use Onestartup\Shop\Model\VariableExtra as Variable;
use Onestartup\Shop\Model\ExtraSaleInfo; use Onestartup\Shop\Model\ExtraSaleInfo;
...@@ -833,6 +834,99 @@ public function shipping() ...@@ -833,6 +834,99 @@ public function shipping()
->with('acordar', 'acordar'); ->with('acordar', 'acordar');
} }
public function openpay()
{
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;
$products = $sale->products;
$items = $sale->items;
$addres = $client->shipping != null ? $client->shipping : null;
$total = $sale->total;
if (request()->coupon != null) {
$data = $this->getDiscount($sale, request()->coupon);
if ($data['valid']) {
$total = $data['total_unformated'];
$sale->coupon = request()->coupon;
}
}
$openpay = new PaymentOpenPay();
$customer = [
'name' => $client->name,
'phone_number' => $client->phone,
'email' => $client->email
];
$total_with_interesteds = $openpay->totalWithPorcent($total, request()->months);
$total_interesteds = $total_with_interesteds - $total;
$payment_detail = [
'method' => 'card',
'source_id' => request()->token_id,
'amount' => $total_with_interesteds,
'description' => 'Pago orden #'.$sale->id.', Meses '.request()->months,
'device_session_id' => request()->deviceIdHiddenFieldName,
'customer' => $customer,
];
$response = $openpay->sendPayment($payment_detail, request()->months);
if ($response['status'] === 'ok') {
$sale->status = 3;
$status = 'process';
if ($response['status_payment'] === 'completed') {
$sale->status = 2;
$status = 'approved';
}
if (request()->months > 1) {
$sale->months = request()->months;
$sale->interests = $total_interesteds;
}
$sale->total = $total_with_interesteds;
$sale->transaction_id = null;
$sale->payment_type = 'Open Pay';
$sale->save();
\Session::forget('client');
\Session::forget('cart');
\Session::forget('sale');
$user = \App\User::first();
//$user->notify(new PaymentAcordarClient('Acordar con el vendedor', $client));
$user->notify(new ResumenOrder($sale, $products, $client, $addres));
$client->notify(new ResumenOrder($sale, $products, $client, $addres));
return redirect()
->route('main.shop')
->with('payment_approved', 'payment_approved');
} else {
return redirect()
->route('cart.finish')
->with('paypal_fail',$response['message']);
}
}
public function testmail() public function testmail()
{ {
/*$sale = Sale::find(10); /*$sale = Sale::find(10);
......
<?php
namespace Onestartup\Shop\Libs;
use Openpay;
class PaymentOpenPay
{
public function sendPayment($payment_data, $months = 1)
{
try{
$openpay = Openpay::getInstance(env('OPENPAY_ID'), env('OPENPAY_SK'));
$charge = array();
$plan = array('payments' => $months);
if ($months >= 3) {
$payment_data['payment_plan'] = $plan;
}
if($charge = $openpay->charges->create($payment_data)){
return ['status'=>'ok', 'status_payment'=>$charge->status, 'message'=>'Pago completado'];
} else {
return ['status'=>'error', 'status_payment'=>'fail', 'message'=>'No se realizo el cargo'];
}
} catch (\OpenpayApiTransactionError $e) {
return ['status'=>'error', 'status_payment'=>'fail_transaction', 'message'=>$e->getMessage()];
} catch (\OpenpayApiRequestError $e) {
return ['status'=>'error', 'status_payment'=>'fail_request', 'message'=>$e->getMessage()];
} catch (\OpenpayApiConnectionError $e) {
return ['status'=>'error', 'status_payment'=>'fail_connection', 'message'=>$e->getMessage()];
} catch (\OpenpayApiAuthError $e) {
return response()->json(['status'=>'error', 'status_payment'=>'fail_auth', 'message'=>$e->getMessage()]);
} catch (\OpenpayApiError $e) {
return ['status'=>'error', 'status_payment'=>'fail_api', 'message'=>$e->getMessage()];
} catch (Exception $e) {
return ['status'=>'error', 'status_payment'=>'fail_script', 'message'=>$e->getMessage()];
}
}
public function totalWithPorcent($total, $months) {
$total_with_porcents = $total;
switch($months)
{
case 1:
$total_with_porcents = $total;
break;
case 3:
$total_with_porcents = (env('TASA_3') / 100) * $total + $total;
break;
case 6:
$total_with_porcents = (env('TASA_6') / 100) * $total + $total;
break;
case 9:
$total_with_porcents = (env('TASA_9') / 100) * $total + $total;
break;
case 12:
$total_with_porcents = (env('TASA_12') / 100) * $total + $total;
break;
default:
$total_with_porcents = $total;
}
return $total_with_porcents;
}
}
\ No newline at end of file
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddInterestedsToSaleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('sale_shops', function (Blueprint $table) {
$table->integer('months')->nullable();
$table->float('interests')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('sale_shops', function (Blueprint $table) {
$table->dropColumn('months');
$table->dropColumn('interests');
});
}
}
...@@ -141,6 +141,7 @@ Route::group(['middleware' => ['web']], function(){ ...@@ -141,6 +141,7 @@ Route::group(['middleware' => ['web']], function(){
Route::post('cart/payment/installments', 'Onestartup\Shop\Controller\CartController@paymentInstallments')->name('cart.payment-installments'); Route::post('cart/payment/installments', 'Onestartup\Shop\Controller\CartController@paymentInstallments')->name('cart.payment-installments');
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::post('cart/acordar', 'Onestartup\Shop\Controller\CartController@acordar')->name('cart.acordar');
Route::post('cart/openpay', 'Onestartup\Shop\Controller\CartController@openpay')->name('cart.openpay');
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]+')
......
...@@ -188,6 +188,15 @@ setlocale(LC_MONETARY, 'en_US'); ...@@ -188,6 +188,15 @@ setlocale(LC_MONETARY, 'en_US');
</tr> </tr>
@endif @endif
<tfoot> <tfoot>
@if($sale->months != null)
<tr>
<td colspan="3" align="right">Meses sin intereses:</td>
<td align="right">
{{money_format('%(#10n', $sale->interests)}}
</td>
</tr>
@endif
<tr> <tr>
<td></td> <td></td>
<td></td> <td></td>
......
...@@ -27,6 +27,9 @@ setlocale(LC_MONETARY, 'en_US'); ...@@ -27,6 +27,9 @@ setlocale(LC_MONETARY, 'en_US');
@if($promo != null) @if($promo != null)
| - | 1 | Descuento *** {{$order->coupon}} *** | {{" - ".$promo['amount']}} | {{" - ".$promo['amount']}}| | - | 1 | Descuento *** {{$order->coupon}} *** | {{" - ".$promo['amount']}} | {{" - ".$promo['amount']}}|
@endif @endif
@if($sale->months != null)
| - | - | - | Meses sin intereses: | {{money_format('%(#10n', $sale->interests)}}|
@endif
| - | - | - | Total: | {{money_format('%(#10n',$order->total)}}| | - | - | - | Total: | {{money_format('%(#10n',$order->total)}}|
@endcomponent @endcomponent
......
<style>
@charset "US-ASCII";
@import "http://fonts.googleapis.com/css?family=Lato:300,400,700";
* {
color: #444;
font-family: Lato;
font-size: 16px;
font-weight: 300;
}
::-webkit-input-placeholder {
font-style: italic;
}
:-moz-placeholder {
font-style: italic;
}
::-moz-placeholder {
font-style: italic;
}
:-ms-input-placeholder {
font-style: italic;
}
body {
float: left;
margin: 0;
padding: 0;
width: 100%;
}
strong {
font-weight: 700;
}
a {
cursor: pointer;
display: block;
text-decoration: none;
}
a.button {
border-radius: 5px 5px 5px 5px;
-webkit-border-radius: 5px 5px 5px 5px;
-moz-border-radius: 5px 5px 5px 5px;
text-align: center;
font-size: 21px;
font-weight: 400;
padding: 12px 0;
width: 100%;
display: table;
background: #E51F04;
background: -moz-linear-gradient(top, #E51F04 0%, #A60000 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#E51F04), color-stop(100%,#A60000));
background: -webkit-linear-gradient(top, #E51F04 0%,#A60000 100%);
background: -o-linear-gradient(top, #E51F04 0%,#A60000 100%);
background: -ms-linear-gradient(top, #E51F04 0%,#A60000 100%);
background: linear-gradient(top, #E51F04 0%,#A60000 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#E51F04', endColorstr='#A60000',GradientType=0 );
}
a.button i {
margin-right: 10px;
}
a.button.disabled {
background: none repeat scroll 0 0 #ccc;
cursor: default;
}
.bkng-tb-cntnt {
float: left;
width: 800px;
}
.bkng-tb-cntnt a.button {
color: #fff;
float: right;
font-size: 18px;
padding: 5px 20px;
width: auto;
}
.bkng-tb-cntnt a.button.o {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
color: #e51f04;
border: 1px solid #e51f04;
}
.bkng-tb-cntnt a.button i {
color: #fff;
}
.bkng-tb-cntnt a.button.o i {
color: #e51f04;
}
.bkng-tb-cntnt a.button.right i {
float: right;
margin: 2px 0 0 10px;
}
.bkng-tb-cntnt a.button.left {
float: left;
}
.bkng-tb-cntnt a.button.disabled.o {
border-color: #ccc;
color: #ccc;
}
.bkng-tb-cntnt a.button.disabled.o i {
color: #ccc;
}
.pymnts {
float: left;
width: 800px;
}
.pymnts * {
float: left;
}
.sctn-row {
margin-bottom: 35px;
width: 800px;
}
.sctn-col {
width: 375px;
}
.sctn-col.l {
width: 425px;
}
.sctn-col input {
border: 1px solid #ccc;
font-size: 18px;
line-height: 24px;
padding: 10px 12px;
width: 333px;
}
.sctn-col label {
font-size: 24px;
line-height: 24px;
margin-bottom: 10px;
width: 100%;
}
.sctn-col.x3 {
width: 300px;
}
.sctn-col.x3.last {
width: 200px;
}
.sctn-col.x3 input {
width: 210px;
}
.sctn-col.x3 a {
float: right;
}
.pymnts-sctn {
width: 800px;
}
.pymnt-itm {
margin: 0 0 3px;
width: 800px;
}
.pymnt-itm h2 {
background-color: #e9e9e9;
font-size: 24px;
line-height: 24px;
margin: 0;
padding: 28px 0 28px 20px;
width: 780px;
}
.pymnt-itm.active h2 {
background-color: #e51f04;
color: #fff;
cursor: default;
}
.pymnt-itm div.pymnt-cntnt {
display: none;
}
.pymnt-itm.active div.pymnt-cntnt {
background-color: #f7f7f7;
display: block;
padding: 0 0 30px;
width: 100%;
}
.pymnt-cntnt div.sctn-row {
margin: 20px 30px 0;
width: 740px;
}
.pymnt-cntnt div.sctn-row div.sctn-col {
width: 345px;
}
.pymnt-cntnt div.sctn-row div.sctn-col.l {
width: 395px;
}
.pymnt-cntnt div.sctn-row div.sctn-col input {
width: 303px;
}
.pymnt-cntnt div.sctn-row div.sctn-col.half {
width: 155px;
}
.pymnt-cntnt div.sctn-row div.sctn-col.half.l {
float: left;
width: 190px;
}
.pymnt-cntnt div.sctn-row div.sctn-col.half input {
width: 113px;
}
.pymnt-cntnt div.sctn-row div.sctn-col.cvv {
background-image: url("./cvv.png");
background-position: 156px center;
background-repeat: no-repeat;
padding-bottom: 30px;
}
.pymnt-cntnt div.sctn-row div.sctn-col.cvv div.sctn-col.half input {
width: 110px;
}
.openpay {
float: right;
height: 60px;
margin: 10px 30px 0 0;
width: 435px;
}
.openpay div.logo {
background-image: url("./openpay.png");
background-position: left bottom;
background-repeat: no-repeat;
border-right: 1px solid #ccc;
font-size: 12px;
font-weight: 400;
height: 45px;
padding: 15px 20px 0 0;
}
.openpay div.shield {
background-image: url("./security.png");
background-position: left bottom;
background-repeat: no-repeat;
font-size: 12px;
font-weight: 400;
margin-left: 20px;
padding: 20px 0 0 40px;
width: 200px;
}
.card-expl {
float: left;
height: 80px;
margin: 20px 0;
width: 800px;
}
.card-expl div {
background-position: left 45px;
background-repeat: no-repeat;
height: 70px;
padding-top: 10px;
}
.card-expl div.debit {
background-image: url("./cards2.png");
margin-left: 20px;
width: 540px;
}
.card-expl div.credit {
background-image: url("./cards1.png");
border-right: 1px solid #ccc;
margin-left: 30px;
width: 209px;
}
.card-expl h4 {
font-weight: 400;
margin: 0;
}
</style>
<div class="bkng-tb-cntnt">
<div class="pymnts">
<form action="{{route('cart.openpay')}}" method="POST" id="payment-form">
{{ csrf_field() }}
<input type="hidden" name="token_id" id="token_id">
<div class="pymnt-itm card active">
<h2>Tarjeta de crédito o débito</h2>
<div class="pymnt-cntnt">
<div class="card-expl">
<div class="credit"><h4>Tarjetas de crédito</h4></div>
<div class="debit"><h4>Tarjetas de débito</h4></div>
</div>
<div class="sctn-row">
<div class="sctn-col l">
<label>Nombre del titular</label><input type="text" placeholder="Como aparece en la tarjeta" autocomplete="off" data-openpay-card="holder_name">
</div>
<div class="sctn-col">
<label>Número de tarjeta</label><input type="text" autocomplete="off" data-openpay-card="card_number"></div>
</div>
<div class="sctn-row">
<div class="sctn-col l">
<label>Fecha de expiración</label>
<div class="sctn-col half l"><input type="text" placeholder="Mes" data-openpay-card="expiration_month"></div>
<div class="sctn-col half l"><input type="text" placeholder="Año" data-openpay-card="expiration_year"></div>
</div>
<div class="sctn-col cvv"><label>Código de seguridad</label>
<div class="sctn-col half l"><input type="text" placeholder="3 dígitos" autocomplete="off" data-openpay-card="cvv2"></div>
</div>
</div>
<div class="sctn-row">
<div class="sctn-col l">
<label>Pago a meses sin intereses <small>(solo tarjetas de crédito)</small></label>
<select id="months" name="months" style="width: 90%; background-color:white;">
<option value="1">1</option>
<option value="3">3</option>
<option value="6">6</option>
</select>
<div id="info-pays">
pago en una sola exhibición
</div>
</div>
<div class="sctn-col">
<label>Si cuentas con un código de promocion introducelo aquí</label>
<input type="text" name="coupon" class="coupon" placeholder="Codigo de promoción" data-total="{{$sale->id}}">
<span class="reponse_coupon"></span>
<br>
</div>
</div>
<div class="openpay"><div class="logo">Transacciones realizadas vía:</div>
<div class="shield">Tus pagos se realizan de forma segura con encriptación de 256 bits</div>
</div>
<div class="sctn-row">
<a class="button rght" id="pay-button">Pagar {{ $sale->home_delivery ? money_format('%(#10n', ($total + $addres->shipping_price->cost)): money_format('%(#10n', $total)}} </a>
</div>
</div>
</div>
</form>
</div>
</div>
@section('scripts_extra')
<script type="text/javascript" src="{{asset('vendor/onestartup/shop/plazos.js')}}"></script>
<script type="text/javascript"
src="https://openpay.s3.amazonaws.com/openpay.v1.min.js"></script>
<script type='text/javascript'
src="https://openpay.s3.amazonaws.com/openpay-data.v1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
OpenPay.setId("{!! env('OPENPAY_ID') !!}");
OpenPay.setApiKey("{!! env('OPENPAY_PK') !!}");
OpenPay.setSandboxMode(true);
var deviceSessionId = OpenPay.deviceData.setup("payment-form", "deviceIdHiddenFieldName");
$('#pay-button').on('click', function(event) {
event.preventDefault();
$("#pay-button").prop( "disabled", true);
OpenPay.token.extractFormAndCreate('payment-form', sucess_callbak, error_callbak);
});
var sucess_callbak = function(response) {
var token_id = response.data.id;
$('#token_id').val(token_id);
$('#payment-form').submit();
};
var error_callbak = function(response) {
var desc = response.data.description != undefined ? response.data.description : response.message;
swal("Error", desc, "error")
$("#pay-button").prop("disabled", false);
};
$( "#months" ).change(getInstallments);
function getInstallments(){
var total = {!! $sale->home_delivery ? ($total + $addres->shipping_price->cost) : $total !!};
var total_with_porcents = 0;
var installment = 0;
var months = parseInt($('#months').val());
switch(months)
{
case 1:
total_with_porcents = 0;
installment = 0;
$('#pay-button').text('Pagar $'+total.toFixed(2));
$('#info-pays').text('pago en una sola exhibición');
break;
case 3:
total_with_porcents = ((parseFloat({!! env('TASA_3') !!}) / 100) * total) + total;
installment = total_with_porcents / months;
$('#pay-button').text('Pagar $'+total_with_porcents.toFixed(2));
$('#info-pays').text(months+' pagos de $'+installment.toFixed(2));
break;
case 6:
total_with_porcents = ((parseFloat({!! env('TASA_6') !!}) / 100) * total) + total;
installment = total_with_porcents / months;
$('#pay-button').text('Pagar $'+total_with_porcents.toFixed(2));
$('#info-pays').text(months+' pagos de $'+installment.toFixed(2));
break;
}
}
});
</script>
@endsection
\ No newline at end of file
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