File: /var/www/vhost/disk-apps/teamdemo.sports-crowd.com/app/Http/Controllers/OpenpayController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Interfaces\PaymentGatewayControllerInterface;
use App\User;
use Exception;
use Openpay\Data\Openpay;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\PaymentGatewayBridgeController;
class OpenpayController extends PaymentGatewayBridgeController implements PaymentGatewayControllerInterface
{
public function setData($gatewayData)
{
parent::setData($gatewayData);
Openpay::setProductionMode($this->is_production);
}
public function index($order)
{
if ($order->gw_state == 'CONFIRMED') {
return $this->webcheckoutDone($order);
}
$this->updatePendingStatus($order, false, true);
return view('openpay.webcheckout')
->with('team', $this->team)
->with('value', $order->total_price)
->with('gateway_payments_id', $this->gatewayData->id)
->with('reference', $order->gw_code_transaction)
->with('user_id', $order->client_id)
->with('description', 'Pago de producto de tienda')
->with('confirmUrl', $this->getConfirmUrl('/store/responseTransaction', 'order', $order->gw_code_transaction));
}
public function ticketIndex($ticket)
{
if ($ticket->payment_state == 'CONFIRMED') {
return $this->webcheckoutDone(null, $ticket);
}
$this->updatePendingStatus($ticket, false, true);
return view('openpay.webcheckout')
->with('team', $this->team)
->with('value', $ticket->total)
->with('gateway_payments_id', $this->gatewayData->id)
->with('reference', $ticket->payment_reference)
->with('user_id', $ticket->user_id_log)
->with('description', 'Pago de boleta o abono')
->with('confirmUrl', $this->getConfirmUrl('/store/responseTransaction', 'ticket', $ticket->payment_reference));
}
public function genericIndex($paymentTransaction, $price, $origin, $clientId, $description)
{
if ($paymentTransaction->state == 'CONFIRMED') {
return $this->webcheckoutDone(null, $paymentTransaction);
}
$this->updatePendingStatus($paymentTransaction, false, true);
return view('openpay.webcheckout')
->with('team', $this->team)
->with('value', $price)
->with('gateway_payments_id', $this->gatewayData->id)
->with('reference', $paymentTransaction->reference)
->with('user_id', $clientId)
->with('description', $description)
->with('confirmUrl', $this->getConfirmUrl('/store/responseTransaction', $origin, $paymentTransaction->reference));
}
public function payment(Request $request)
{
$user = User::where('id', $request['user_id'])->first();
$customer = array(
'name' => $user->first_name,
'last_name' => $user->last_name,
'phone_number' => $user->phone,
'email' => $user->email
);
$publicIp = $this->getPublicIp($request);
$this->createCharge(
$request['value'],
$request['confirmUrl'],
$request['method'],
$request['reference'],
$customer,
$request['description'],
$publicIp
);
}
private function createCharge($value, $confirmUrl, $method, $reference, $customer, $description, $publicIp)
{
$openpay = Openpay::getInstance($this->gatewayData->merchant_id, $this->privateKey, 'CO', $publicIp);
try {
$chargeRequest = array(
"method" => $method,
'amount' => $value,
'description' => $description,
'customer' => $customer,
'currency' => 'COP',
'order_id' => $reference
);
if ($method != 'store') {
$chargeRequest['send_email'] = true;
$chargeRequest['confirm'] = false;
$chargeRequest['redirect_url'] = $confirmUrl;
}
$charge = $openpay->charges->create($chargeRequest);
$this->updatePayment($reference, 'PENDING', 1, 'Actualización transaction id', $charge->id);
$this->redirectCharge($charge);
} catch (Exception $e) {
if ($e->getCode() == 1006) {
echo "<script>window.close();</script>";
} else {
throw $e;
}
}
}
public function responseTransaction(Request $request)
{
$webcheckoutConfirmData = $this->validatePayment($request['id'], $request['reference'], $request->ip());
$webcheckoutConfirmData['origin'] = $request['origin'];
return $this->webcheckoutConfirm($webcheckoutConfirmData);
}
public function webhooksListener(Request $request)
{
$utilController = new UtilController();
$utilController->logFile($request);
if ($request->getContent() == null) {
return response(array('r' => true, 'm' => "La petición no tine contenido", 'd' => null));
}
$requestContent = json_decode($request->getContent(), true);
if ($requestContent['transaction'] == null) {
return response(array(
'r' => true,
'm' => "La petición no tiene información de la transacción",
'd' => null
));
}
$transaction = $requestContent['transaction'];
$paymentReference = $transaction['order_id'];
if (!isset($paymentReference)) {
return response(array('r' => false, 'm' => "Recibido", 'd' => 'El evento no tiene referenceCode'));
}
switch ($requestContent['type']) {
case 'charge.succeeded':
$this->updatePayment(
$paymentReference,
'CONFIRMED',
2,
'Transacción confirmada por webhook',
$transaction['id']
);
break;
case 'charge.failed':
case 'charge.cancelled':
$this->updatePayment(
$paymentReference,
'REJECTED',
7,
$transaction['error_message'],
$transaction['id']
);
break;
case 'transaction.expired':
$this->updatePayment(
$paymentReference,
'EXPIRED',
7,
'Transacción expirada',
$transaction['id']
);
break;
}
return response(array('r' => true, 'm' => "Recibido", 'd' => null));
}
public function getTransactionByReference(Request $request)
{
$charge = $this->getChargeById($request["refTransaction_value"], $request->ip());
return response()->json(array(
'r' => true,
'm' => 'Transacción obtenida con éxito',
'd' => ((array) $charge)
));
}
public function getChargeById($id, $publicIp)
{
$openpay = Openpay::getInstance($this->gatewayData->merchant_id, $this->privateKey, 'CO', $publicIp);
return $openpay->charges->get($id);
}
public function redirectCharge($charge)
{
if ($charge->payment_method->type == 'redirect') {
$redirectUrl = $charge->payment_method->url;
header("Location: " . $redirectUrl);
exit();
}
if ($charge->payment_method->type == 'store') {
$redirectUrl = $this->urlApi . '/paynet-pdf/' . $this->gatewayData->merchant_id . '/' . $charge->payment_method->reference;
return Redirect::away($redirectUrl);
}
}
public function validatePayment($transactionId, $reference)
{
if ($transactionId == null) {
return;
}
$publicIp = $this->getPublicIp();
try {
$charge = $this->getChargeById($transactionId, $publicIp);
$webcheckoutConfirmData = $webcheckoutConfirmData = [
'state' => 'PENDING',
'comment' => 'La transacción esta siendo procesada',
];
switch ($charge->status) {
case 'completed':
$this->updatePayment(
$charge->order_id,
'CONFIRMED',
2,
'Transacción confirmada por validación',
$transactionId
);
$webcheckoutConfirmData = [
'state' => 'CONFIRMED',
'comment' => 'Transacción confirmada por validación',
];
break;
case 'failed':
$this->updatePayment($charge->order_id, 'FAILED', 7, $charge->error_message, $transactionId);
$webcheckoutConfirmData = [
'state' => 'FAILED',
'comment' => $charge->error_message,
];
break;
case 'cancelled':
$this->updatePayment($charge->order_id, 'CANCELLED', 7, $charge->error_message, $transactionId);
$webcheckoutConfirmData = [
'state' => 'CANCELLED',
'comment' => $charge->error_message,
];
break;
case 'expired':
$this->updatePayment($charge->order_id, 'EXPIRED', 7, 'Transacción expirada', $transactionId);
$webcheckoutConfirmData = [
'state' => 'EXPIRED',
'comment' => 'Transacción expirada',
];
break;
}
return $webcheckoutConfirmData;
} catch (Exception $e) {
return;
}
}
public function getPublicIp(Request $request = null)
{
return $request ? $request->ip() : file_get_contents('http://ipecho.net/plain');
}
public function getAuthorizationCode($gatewayResponse)
{
return null;
}
public function getPaymentMethod($gatewayResponse){
return null;
}
}