File "BkashPayment.php"
Full path: /home/orbitecv/orbite-school.orbit.et/app/Services/BkashPayment.php
File size: 3.81 KiB (3903 bytes)
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Edit Back
<?php
namespace App\Services;
use App\Interfaces\PaymentGatewayInterface;
use App\Models\PaymentGateway;
use App\Repositories\TransactionRepository;
class BkashPayment implements PaymentGatewayInterface
{
public function processPayment($amount, array $data, $config)
{
$paymentGateway = PaymentGateway::where('name', 'bkash')->first();
$token = $paymentGateway->token;
$status = '0000';
if (!$token || !$paymentGateway->token_expires_at || $paymentGateway->token_expires_at < now()) {
$response = json_decode(self::tokenGrant($config));
$generateExpiredTime = now()->addMinutes(59);
$paymentGateway->update([
'token' => $response->id_token,
'token_expires_at' => $generateExpiredTime,
]);
$status = $response->statusCode;
$token = $response->id_token;
}
$transactionId = $data['product']['transaction_id'];
$transaction = TransactionRepository::query()->where('id', $transactionId)->first();
$transaction->update([
'token' => $token,
]);
if ($status != '0000') {
return json_encode(['error' => $status]);
}
$callbackUrl = route('bkash.payment.execute', ['data' => $data['identifier'], 'token' => $token]);
$url = 'https://tokenized.sandbox.bka.sh/v1.2.0-beta/tokenized/checkout/create';
if ($config['mode'] == 'live') {
$url = 'https://tokenized.pay.bka.sh/v1.2.0-beta/tokenized/checkout/create';
}
$requestbody = [
'mode' => '0011',
'amount' => $amount,
'currency' => 'BDT',
'intent' => 'sale',
'payerReference' => '01',
'merchantInvoiceNumber' => 'TRX' . str_pad($transactionId, 6, '0', STR_PAD_LEFT) . '-' . rand(1111, 9999),
'callbackURL' => $callbackUrl,
];
$header = [
'Content-Type:application/json',
'Accept:application/json',
'Authorization:' . $token,
'X-APP-Key:' . $config['app_key'],
];
$resultData = self::executeCurl($url, $requestbody, $header);
$resultData = json_decode($resultData);
if (isset($resultData->bkashURL)) {
return redirect()->away($resultData->bkashURL);
}
// error handling
return json_encode(['error' => $resultData]);
}
/**
* bKash token grant
*
* @return string
*/
private static function tokenGrant($config)
{
$url = 'https://tokenized.sandbox.bka.sh/v1.2.0-beta/tokenized/checkout/token/grant';
if ($config['mode'] == 'live') {
$url = 'https://tokenized.pay.bka.sh/v1.2.0-beta/tokenized/checkout/token/grant';
}
$authorizableData = [
'app_key' => $config['app_key'],
'app_secret' => $config['app_secret_key'],
];
$header = [
'Content-Type:application/json',
'Accept:application/json',
'username:' . $config['username'],
'password:' . $config['password'],
];
return self::executeCurl($url, $authorizableData, $header);
}
/**
* bKash payment execute eCurl
*
* @return string
*/
private static function executeCurl($url, $data, $header)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}