Skip to content

Instantly share code, notes, and snippets.

@gusLopezC
Last active November 12, 2021 05:10
Show Gist options
  • Save gusLopezC/c287842377cd2c285c1b5d29eda21feb to your computer and use it in GitHub Desktop.
Save gusLopezC/c287842377cd2c285c1b5d29eda21feb to your computer and use it in GitHub Desktop.
public function charge_card(Request $request)
{
// $cart = Cart::where('user_id', auth()->user()->id)->where('order_id', null)->get()->toArray();
$customer_id = $this->validateCustomer();
$request->total = str_replace(",", "", $request->total);
$orden = $this->create_OrderID();
$chargeRequest = array(
'method' => 'card',
'source_id' => $request->token_id,
'amount' => $request->total,
"currency" => "MXN",
'description' => 'Cargo' . $orden,
'device_session_id' => $request->deviceSessionId,
// 'order_id' => $orden,
'use_card_points' => false, //$request->use_card_points,
'capture' => true, //$request->capture,
// 'use_3d_secure' => true,
// 'redirect_url' => 'http://127.0.0.1:8000/checkout/checkout_confirmation'
//'redirect_url' => 'https://gshopdemo.herokuapp.com/checkout/checkout_confirmation'
);
if ($request->msi > 1) {
$chargeRequest['payment_plan'] = array('payments' => (int)$request->msi);
}
try {
$customer = $this->openpay->customers->get($customer_id);
$charge = $customer->charges->create($chargeRequest);
// return Redirect::to($charge->payment_method->url);
return response()->json([
'id' => $charge->id,
'status' => $charge->status,
'payment_method' => $charge->method
], 200);
} catch (Exception $e) {
$error = $this->error($e);
return response()->json(['error' => $error, "descripcion" => $e->getDescription()], 404);
}
}
public function getOrder($id){
$customer_id = $this->validateCustomer();
$customer = $this->openpay->customers->get($customer_id);
$charge = $customer->charges->get($id);
return $charge;
}
public function refund(Request $request, $order_id)
{
try {
$customer = $this->openpay->customers->get($user->openpay_customer);
$charge = $customer->charges->get($order->payment_trx_id);
$charge->refund($refundData);
if ($charge) {
$order->payment_status = "refund";
$order->status = "cancel";
$order->save();
// Mail
// Mail::to($user->email)->send(new OrderRefund($charge));
request()->session()->flash('success', 'Reembolso realizado completamente ');
if($request->restock){
$orden = new ProductController;
$refund =$orden->refundStockProducts($order->id);
}
return redirect()->back();
}
} catch (Exception $e) {
return $e;
request()->session()->flash('error', 'Su peticiòn no pudo ser procesada ' . $this->error($e));
return redirect()->back();
}
}
public function error(Exception $e)
{
/* 6001 el webhook ya existe */
switch ($e->getCode()) {
/* ERRORES GENERALES */
case '1000':
case '1004':
case '1005':
$msg = 'Servicio no disponible.';
break;
/* ERRORES TARJETA */
case '1006':
$msg = 'Ya existe una transacción con el mismo ID de orden..';
break;
case '3001':
case '3004':
case '3005':
case '3007':
$msg = 'La tarjeta fue rechazada.';
break;
case '3002':
$msg = 'La tarjeta ha expirado.';
break;
case '3003':
$msg = 'La tarjeta no tiene fondos suficientes.';
break;
case '3006':
$msg = 'La operación no esta permitida para este cliente o esta transacción.';
break;
case '3008':
$msg = 'La tarjeta no es soportada en transacciones en línea.';
break;
case '3009':
$msg = 'La tarjeta fue reportada como perdida.';
break;
case '3010':
$msg = 'El banco ha restringido la tarjeta.';
break;
case '3011':
$msg = 'El banco ha solicitado que la tarjeta sea retenida. Contacte al banco.';
break;
case '3012':
$msg = 'Se requiere solicitar al banco autorización para realizar este pago.';
break;
default: /* Demás errores 400 */
$msg = 'La petición no pudo ser procesada.';
break;
}
$error = 'ERROR ' . $e->getCode() . '. ' . $msg;
return $error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment