diff --git a/config/payment.php b/config/payment.php index a372534..782b4d4 100755 --- a/config/payment.php +++ b/config/payment.php @@ -357,6 +357,7 @@ 'merchantId' => '', 'callbackUrl' => 'http://yoursite.com/path/to', 'description' => 'payment using zibal', + 'referer' => null, 'currency' => IranCurrency::TOMAN, ], 'sepordeh' => [ diff --git a/src/Drivers/Zibal/Zibal.php b/src/Drivers/Zibal/Zibal.php index 83d32ba..12bfd46 100644 --- a/src/Drivers/Zibal/Zibal.php +++ b/src/Drivers/Zibal/Zibal.php @@ -87,6 +87,9 @@ public function purchase(): string|int|null RequestOptions::BODY => json_encode($data), RequestOptions::HEADERS => [ 'Content-Type' => 'application/json', + ...(($this->settings->referer ?? null) === null ? [] : [ + 'Referer' => $this->settings->referer, + ]), ], RequestOptions::HTTP_ERRORS => false, ] @@ -153,6 +156,9 @@ public function verify(): ReceiptInterface RequestOptions::HEADERS => [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', + ...(($this->settings->referer ?? null) === null ? [] : [ + 'Referer' => $this->settings->referer, + ]), ], RequestOptions::HTTP_ERRORS => false, ] diff --git a/tests/Drivers/ZibalTest.php b/tests/Drivers/ZibalTest.php index d113c72..c0b3264 100644 --- a/tests/Drivers/ZibalTest.php +++ b/tests/Drivers/ZibalTest.php @@ -85,6 +85,26 @@ public function testPurchaseMergesTheOptionalFieldsIntoTheRequest(): void $this->assertSame('0010000000', $this->requestJson()['nationalCode']); } + public function testPurchaseSendsTheConfiguredReferer(): void + { + $driver = $this->driver(['referer' => 'https://example.com']); + $this->fakeHttp($driver, [$this->jsonResponse(['result' => 100, 'trackId' => 1])]); + + $driver->amount(1000)->purchase(); + + $this->assertSame('https://example.com', $this->request()->getHeaderLine('Referer')); + } + + public function testPurchaseOmitsTheRefererByDefault(): void + { + $driver = $this->driver(); + $this->fakeHttp($driver, [$this->jsonResponse(['result' => 100, 'trackId' => 1])]); + + $driver->amount(1000)->purchase(); + + $this->assertFalse($this->request()->hasHeader('Referer')); + } + public function testPurchaseFailsWhenTheGatewayReportsAnError(): void { $driver = $this->driver(); @@ -143,6 +163,34 @@ public function testVerifyReturnsAReceipt(): void $this->assertSame(1234567, $this->requestJson()['trackId']); } + public function testVerifySendsTheConfiguredReferer(): void + { + $this->fakeRequest(['success' => 1, 'trackId' => 1234567]); + + $driver = $this->driver(['referer' => 'https://example.com']); + $this->fakeHttp($driver, [ + $this->jsonResponse(['result' => 100, 'refNumber' => 'ref-1']), + ]); + + $driver->verify(); + + $this->assertSame('https://example.com', $this->request()->getHeaderLine('Referer')); + } + + public function testVerifyOmitsTheRefererByDefault(): void + { + $this->fakeRequest(['success' => 1, 'trackId' => 1234567]); + + $driver = $this->driver(); + $this->fakeHttp($driver, [ + $this->jsonResponse(['result' => 100, 'refNumber' => 'ref-1']), + ]); + + $driver->verify(); + + $this->assertFalse($this->request()->hasHeader('Referer')); + } + public function testVerifyFailsWhenThePaymentWasNotSuccessful(): void { $this->fakeRequest(['success' => 0, 'status' => 3]);