Commit d3a2de64 by Angel MAS

open pay integration

parent 535db319
......@@ -37,6 +37,7 @@ use PayPal\Api\ItemList;
use PayPal\Api\Details;
use Onestartup\Shop\Libs\Util;
use Onestartup\Shop\Libs\PaymentOpenPay;
use Onestartup\Shop\Model\VariableExtra as Variable;
use Onestartup\Shop\Model\ExtraSaleInfo;
......@@ -833,6 +834,99 @@ public function shipping()
->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()
{
/*$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(){
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/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')
->where('quantity', '[0-9]+')
......
......@@ -188,6 +188,15 @@ setlocale(LC_MONETARY, 'en_US');
</tr>
@endif
<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>
<td></td>
<td></td>
......
......@@ -27,6 +27,9 @@ setlocale(LC_MONETARY, 'en_US');
@if($promo != null)
| - | 1 | Descuento *** {{$order->coupon}} *** | {{" - ".$promo['amount']}} | {{" - ".$promo['amount']}}|
@endif
@if($sale->months != null)
| - | - | - | Meses sin intereses: | {{money_format('%(#10n', $sale->interests)}}|
@endif
| - | - | - | Total: | {{money_format('%(#10n',$order->total)}}|
@endcomponent
......
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