Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
'merchantId' => '',
'callbackUrl' => 'http://yoursite.com/path/to',
'description' => 'payment using zibal',
'referer' => null,
'currency' => IranCurrency::TOMAN,
],
'sepordeh' => [
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Zibal/Zibal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand Down Expand Up @@ -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,
]
Expand Down
48 changes: 48 additions & 0 deletions tests/Drivers/ZibalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]);
Expand Down
Loading