get('payment.gateway'), $options); $this->endpoint = rtrim($finalConfig['endpoint'], '/'); $this->merchant_id = $finalConfig['merchant_id']; $this->secret_key = $finalConfig['secret_key']; $this->keyMap = $finalConfig['key_map']; } public function createPaymentURL(PayableEntity $payable, array $options = []): string { return "$this->endpoint/payment/$this->merchant_id?" . http_build_query($this->preparePaymentParam($payable, $options)); } public function verifyGatewayReturn(Request $request): bool { $hash = $request->query->get($this->keyMap['hash']); return $hash == $this->computeHash($request->request->all()); } public function verifyGatewayCallback(Request $request, Response &$response = null): bool { $response = Res::make('OK'); $hash = $request->request->get($this->keyMap['hash']); return $hash == $this->computeHash($request->request->all()); } public function getBillIdFromRequest(Request $request): ?string { return $request->get($this->keyMap['order_id']); } public function getBillStatus(Request $request): ?int { $status = $request->get($this->keyMap['status_id']); if ($status == null) { return null; } else { return $status == 1 ? Payment::SUCCESS : Payment::PENDING; } } public function computeHash($payload) { $query = http_build_query($payload); $key = preg_quote(http_build_query([$this->keyMap['hash']=>'']), '/'); $query = preg_replace('/(^|&)'.$key.'[^&]*($|&)/','$1hash=[HASH]$2', $query); return md5($this->secret_key . '?' . $query); } public function isPostMethodSupported(): bool { return true; } public function createPostMethodPayment(PayableEntity $payable, array $options = []): Response { $params = $this->preparePaymentParam($payable, $options); ob_start(); include __DIR__.'../res/view/response.php'; return new Response(ob_get_clean()); } protected function preparePaymentParam(PayableEntity $payable, array $options = []): array{ $detail = $payable->getBillDescription(); $amount = $payable->getBillAmount(); $order_id = $payable->getBillId(); $detail = preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail); $amount = sprintf('%0.2f', $amount); $hash = md5($this->secret_key . $detail . $amount . $order_id); return compact('detail', 'order_id', 'amount', 'hash'); } }