123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
-
-
- namespace MirfalahTech\Laravel\Payment\Gateway\RazerPay;
-
-
- use Illuminate\Contracts\Config\Repository;
- use Illuminate\Contracts\Events\Dispatcher;
- use Illuminate\Http\Request;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Response as Res;
- use MirfalahTech\Laravel\Payment\Contracts\PayableEntity;
- use MirfalahTech\Laravel\Payment\Contracts\PaymentGatewayDriver;
- use MirfalahTech\Laravel\Payment\Events\CallbackVerifiedEvent;
- use MirfalahTech\Laravel\Payment\Events\PaymentFailedEvent;
- use MirfalahTech\Laravel\Payment\Events\PaymentPendingEvent;
- use MirfalahTech\Laravel\Payment\Events\PaymentSuccessEvent;
- use MirfalahTech\Laravel\Payment\Events\ReturnVerifiedEvent;
- use MirfalahTech\Laravel\Payment\Facade\Payment;
- use MirfalahTech\Laravel\Payment\Traits\BillStatusBoolean;
- use Symfony\Component\HttpFoundation\Response;
-
- class RazerPayDriver implements PaymentGatewayDriver
- {
- use BillStatusBoolean;
-
- /**
- * @var string
- */
- protected $endpoint;
-
- /**
- * @var string
- */
- protected $merchant_id;
-
- /**
- * @var string
- */
- protected $secret_key;
- /**
- * @var string
- */
- protected $verify_key;
-
- /**
- * @var Repository
- */
- protected $config;
-
- /**
- * @var Dispatcher
- */
- protected $events;
-
- public function __construct(Repository $config, Dispatcher $events)
- {
- $this->config = $config;
- $this->events = $events;
-
- $this->endpoint = rtrim($config->get('payment.gateway.razerpay.endpoint'), '/');
- $this->merchant_id = $config->get('payment.gateway.razerpay.merchant_id');
- $this->secret_key = $config->get('payment.gateway.razerpay.secret_key');
- $this->verify_key = $config->get('payment.gateway.razerpay.verify_key');
- }
-
- public function createPaymentURL(PayableEntity $payable, array $options = []): string
- {
- $orderid = urlencode($payable->getBillId());
- $bill_name = urlencode($payable->getBillName());
- $bill_email = $payable->getBillEmail();
- $bill_mobile = $payable->getBillPhoneNumber();
- $detail = urlencode($payable->getBillDescription());
- $country = $payable->getBillCountry();
- $bill_desc = urlencode(preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail));
- $amount = sprintf('%0.2f', $payable->getBillAmount());
- $currency = $payable->getBillCurrency();
- $vcode = md5($amount . $this->merchant_id . $payable->getBillId() . $this->verify_key);
-
- $query = array_merge(
- compact(
- 'amount', 'orderid', 'bill_name', 'bill_email', 'bill_mobile',
- 'bill_desc', 'country', 'vcode', 'currency'
- ),
- Arr::only($this->config->get('payment.gateway.razerpay.options', []), ['channel', 'returnurl', 'callbackurl']),
- Arr::only($options, ['channel', 'returnurl', 'callbackurl'])
- );
- return "$this->endpoint/MOLPay/pay/$this->merchant_id/?" .
- http_build_query($query);
- }
-
- public function verifyGatewayReturn(Request $request): bool
- {
- if ($this->verifyRequest($request)) {
- $this->events->dispatch(new ReturnVerifiedEvent('razerpay', $this, $request));
- $this->dispatchStatusEvent($request);
- return true;
- }
- return false;
- }
-
- public function verifyGatewayCallback(Request $request, Response &$response = null): bool
- {
- $response = $request->request->has('nbcb') ?
- Res::make('CBTOKEN:MPSTATOK') :
- Res::make('OK');
-
- if ($this->verifyRequest($request)) {
- $this->events->dispatch(new CallbackVerifiedEvent('razerpay', $this, $request));
- $this->dispatchStatusEvent($request);
- return true;
- }
- return false;
- }
-
- protected function verifyRequest(Request $request): bool
- {
- $tranID = $request->request->get('tranID');
- $order_id = $request->request->get('orderid');
- $status = $request->request->get('status');
- $domain = $request->request->get('domain');
- $amount = $request->request->get('amount');
- $currency = $request->request->get('currency');
- $appcode = $request->request->get('appcode');
- $pay_date = $request->request->get('paydate');
-
- $key0 = md5($tranID . $order_id . $status . $domain . $amount . $currency);
-
- $key1 = md5($pay_date . $domain . $key0 . $appcode . $this->secret_key);
-
- return $key1 == $request->request->get('skey');
- }
-
- protected function dispatchStatusEvent(Request $request)
- {
- switch ($this->getBillStatus($request)) {
- case Payment::SUCCESS:
- $this->events->dispatch(new PaymentSuccessEvent('razerpay', $this, $request));
- break;
- case Payment::PENDING:
- $this->events->dispatch(new PaymentPendingEvent('razerpay', $this, $request));
- break;
- case Payment::FAILED:
- $this->events->dispatch(new PaymentFailedEvent('razerpay', $this, $request));
- break;
- }
- }
-
- public function getBillIdFromRequest(Request $request): ?string
- {
- return $request->request->get('orderid');
- }
-
- public function getBillStatus(Request $request): ?int
- {
- if ($request->request->has('status')) {
- $status = $request->request->get('status');
- switch ($status){
- case '00':
- return Payment::SUCCESS;
- case '11':
- return Payment::FAILED;
- default:
- return Payment::PENDING;
- }
- } else {
- return null;
- }
- }
- }
|