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; } } }