Skip to content

Commit 86313a4

Browse files
authored
Merge pull request #44 from XShaan/master
Added Zibal & Pay.ir Gateways
2 parents 4b989f9 + 8cee21a commit 86313a4

File tree

12 files changed

+433
-24
lines changed

12 files changed

+433
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Here are a few short examples of what you can do:
3434
- Pay.ir Gateway / درگاه پرداخت پی
3535
- Zarinpal Gateway / درگاه پرداخت زرین پال
3636
- IDPay Gateway / درگاه آیدی پی
37+
- Zibal Gateway / درگاه زیبال
38+
3739
- ...
3840
- Other gateways, coming soon... لطفا شما هم در تکمیل پکیج مشارکت کنید
3941

config/larapay.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
|--------------------------------------------------------------------------
118118
| Pay.ir gateway configuration
119119
|--------------------------------------------------------------------------
120+
|
121+
| api: For the sandbox gateway, set API to 'test'
122+
|
120123
*/
121124
'payir' => [
122125
'api' => env('PAY_IR_API_KEY', ''),
@@ -139,6 +142,23 @@
139142
'description' => env('IDPAY_DESCRIPTION', 'powered-by-Larapay'),
140143
],
141144

145+
/*
146+
|--------------------------------------------------------------------------
147+
| Zibal gateway configuration
148+
|--------------------------------------------------------------------------
149+
|
150+
| merchant_id: For the sandbox gateway, set merchant_id to 'zibal'
151+
|
152+
*/
153+
'zibal' => [
154+
'merchant_id' => env('ZIBAL_MERCHANT_ID', ''),
155+
'type' => env('ZIBAL_TYPE', 'normal'),
156+
'callback_url' => env('ZIBAL_CALLBACK_URL', ''),
157+
'email' => env('ZIBAL_EMAIL', ''),
158+
'mobile' => env('ZIBAL_MOBILE', '09xxxxxxxxx'),
159+
'description' => env('ZIBAL_DESCRIPTION', 'powered-by-Larapay'),
160+
],
161+
142162
/*
143163
|--------------------------------------------------------------------------
144164
| SoapClient Options

src/Adapter/PayIr/Exception.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace Tartan\Larapay\Adapter\PayIr;
44

55
class Exception extends \Tartan\Larapay\Adapter\Exception {
6-
protected $adapter = 'pay_ir';
7-
}
6+
protected $adapter = 'payir';
7+
}

src/Adapter/PayIr/Helper.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Tartan\Larapay\Adapter\PayIr;
3+
4+
use Tartan\Log\Facades\XLog;
5+
6+
class Helper
7+
{
8+
/**
9+
* CURL POST TO HTTPS
10+
*
11+
* @param $fields_arr
12+
* @param $url
13+
* @return mixed
14+
*/
15+
public static function post2https($fields_arr, $url)
16+
{
17+
$ch = curl_init();
18+
//set the url, number of POST vars, POST data
19+
curl_setopt($ch, CURLOPT_URL, $url);
20+
curl_setopt($ch, CURLOPT_POST, count($fields_arr));
21+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_arr);
22+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
23+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
24+
25+
//execute post
26+
$res = curl_exec($ch);
27+
28+
//close connection
29+
curl_close($ch);
30+
31+
XLog::debug('Payir call result: '. $res);
32+
return $res;
33+
}
34+
}

src/Adapter/Payir.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Tartan\Larapay\Adapter;
55

66
use Tartan\Larapay\Adapter\Zarinpal\Exception;
7+
use Tartan\Larapay\Adapter\PayIr\Helper;
78
use Tartan\Log\Facades\XLog;
89

910
/**
@@ -13,9 +14,9 @@
1314
class Payir extends AdapterAbstract implements AdapterInterface
1415
{
1516

16-
public $endPoint = 'https://pay.ir/payment/send';
17-
public $endPointForm = 'https://pay.ir/payment/gateway/';
18-
public $endPointVerify = 'https://pay.ir/payment/verify';
17+
public $endPoint = 'https://pay.ir/pg/send';
18+
public $endPointForm = 'https://pay.ir/pg/';
19+
public $endPointVerify = 'https://pay.ir/pg/verify';
1920

2021
public $reverseSupport = false;
2122

@@ -40,7 +41,7 @@ protected function requestToken(): string
4041
$sendParams = [
4142
'api' => $this->api,
4243
'amount' => intval($this->amount),
43-
'factorNumber' => ($this->order_id),
44+
'orderId' => ($this->order_id),
4445
'description' => $this->description ? $this->description : '',
4546
'mobile' => $this->mobile ? $this->mobile : '',
4647
'redirect' => $this->redirect_url,
@@ -50,17 +51,17 @@ protected function requestToken(): string
5051
XLog::debug('PaymentRequest call', $sendParams);
5152
$result = Helper::post2https($sendParams, $this->endPoint);
5253

53-
5454
$resultObj = json_decode($result);
55+
5556
XLog::info('PaymentRequest response', $this->obj2array($resultObj));
5657

5758

5859
if (isset($resultObj->status)) {
5960

6061
if ($resultObj->status == 1) {
61-
$this->getTransaction()->setGatewayToken(strval($resultObj->transId)); // update transaction reference id
62+
$this->getTransaction()->setGatewayToken(strval($resultObj->token)); // update transaction reference id
6263

63-
return $resultObj->transId;
64+
return $resultObj->token;
6465
} else {
6566
throw new Exception($resultObj->status);
6667
}
@@ -111,45 +112,40 @@ public function formParams(): array
111112
*/
112113
protected function verifyTransaction(): bool
113114
{
115+
114116
if ($this->getTransaction()->checkForVerify() == false) {
115117
throw new Exception('larapay::larapay.could_not_verify_payment');
116118
}
117119

118120
$this->checkRequiredParameters([
119121
'api',
120-
'transId',
122+
'token',
121123
]);
122124

123125
$sendParams = [
124126
'api' => $this->api,
125-
'transId' => $this->transId,
127+
'token' => $this->token,
126128
];
127129

128130
try {
129-
130131
XLog::debug('PaymentVerification call', $sendParams);
131132
$result = Helper::post2https($sendParams, $this->endPointVerify);
132133
$response = json_decode($result);
133134
XLog::info('PaymentVerification response', $this->obj2array($response));
134135

135-
136136
if (isset($response->status, $response->amount)) {
137-
138137
if ($response->status == 1) {
139138
$this->getTransaction()->setVerified();
140-
$this->getTransaction()->setReferenceId(strval($this->transId)); // update transaction reference id
141-
139+
$this->getTransaction()->setReferenceId(strval($this->token)); // update transaction reference id
142140
return true;
143141
} else {
144142
throw new Exception($response->status);
145143
}
146144
} else {
147-
throw new Exception('larapay::larapay.invalid_response');
145+
throw new Exception($response->status);
148146
}
149-
150147
} catch (\Exception $e) {
151-
152-
throw new Exception('PayIr: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
148+
throw new Exception('Payir Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
153149
}
154150
}
155151

@@ -158,8 +154,7 @@ protected function verifyTransaction(): bool
158154
*/
159155
public function canContinueWithCallbackParameters(): bool
160156
{
161-
162-
if (!empty($this->parameters['transId'])) {
157+
if (!empty($this->parameters['token'])) {
163158
return true;
164159
}
165160

@@ -173,9 +168,9 @@ public function canContinueWithCallbackParameters(): bool
173168
public function getGatewayReferenceId(): string
174169
{
175170
$this->checkRequiredParameters([
176-
'transId',
171+
'token',
177172
]);
178173

179174
return strval($this->transId);
180175
}
181-
}
176+
}

src/Adapter/Zibal.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tartan\Larapay\Adapter;
5+
6+
use Tartan\Larapay\Adapter\Zibal\Exception;
7+
use Tartan\Larapay\Adapter\Zibal\Helper;
8+
use Tartan\Log\Facades\XLog;
9+
10+
11+
/**
12+
* Class Zibal
13+
* @package Tartan\Larapay\Adapter
14+
*/
15+
class Zibal extends AdapterAbstract implements AdapterInterface
16+
{
17+
protected $WSDL = 'https://gateway.zibal.ir/v1/request';
18+
protected $endPoint = 'https://gateway.zibal.ir/start/{trackId}';
19+
20+
protected $testWSDL = 'https://gateway.zibal.ir/v1/request';
21+
protected $testEndPoint = 'https://gateway.zibal.ir/start/{trackId}';
22+
23+
public $endPointVerify = 'https://gateway.zibal.ir/v1/verify';
24+
25+
public $reverseSupport = false;
26+
27+
/**
28+
* @return string
29+
* @throws Exception
30+
* @throws \Tartan\Larapay\Adapter\Exception
31+
*/
32+
protected function requestToken(): string
33+
{
34+
if ($this->getTransaction()->checkForRequestToken() == false) {
35+
throw new Exception('larapay::larapay.could_not_request_payment');
36+
}
37+
38+
$this->checkRequiredParameters([
39+
'merchant_id',
40+
'amount',
41+
'redirect_url',
42+
]);
43+
44+
$sendParams = [
45+
'merchant' => $this->getSandbox(),
46+
'orderId' => $this->getTransaction()->bank_order_id,
47+
'amount' => intval($this->amount),
48+
'description' => $this->description ? $this->description : '',
49+
'mobile' => $this->mobile ? $this->mobile : '',
50+
'callbackUrl' => $this->redirect_url,
51+
];
52+
53+
try {
54+
XLog::debug('PaymentRequest call', $sendParams);
55+
$result = Helper::post2https($sendParams, $this->WSDL);
56+
$resultObj = json_decode($result);
57+
58+
XLog::info('PaymentRequest response', $this->obj2array($resultObj));
59+
60+
if (isset($resultObj->result)) {
61+
if ($resultObj->result == 100) {
62+
$this->getTransaction()->setGatewayToken(strval($resultObj->trackId)); // update transaction reference id
63+
return strval($resultObj->trackId);
64+
} else {
65+
throw new Exception($resultObj->result);
66+
}
67+
} else {
68+
throw new Exception('larapay::larapay.invalid_response');
69+
}
70+
} catch (\Exception $e) {
71+
throw new Exception($e->getMessage());
72+
}
73+
74+
}
75+
76+
77+
/**
78+
* @return string
79+
* @throws Exception
80+
* @throws \Tartan\Larapay\Adapter\Exception
81+
*/
82+
protected function generateForm(): string
83+
{
84+
85+
$authority = $this->requestToken();
86+
87+
$form = view('larapay::zibal-form', [
88+
'endPoint' => strtr($this->getEndPoint(), ['{trackId}' => $authority]),
89+
'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"),
90+
'autoSubmit' => boolval($this->auto_submit),
91+
]);
92+
93+
return $form->__toString();
94+
}
95+
96+
/**
97+
* @return array
98+
* @throws Exception
99+
* @throws \Tartan\Larapay\Adapter\Exception
100+
*/
101+
public function formParams(): array
102+
{
103+
$authority = $this->requestToken();
104+
105+
return [
106+
'endPoint' => strtr($this->getEndPoint(), ['{authority}' => $authority]),
107+
];
108+
}
109+
110+
public function getSandbox(): string
111+
{
112+
if (config('larapay.mode') == 'production') {
113+
return $this->merchant_id;
114+
} else {
115+
return "zibal";
116+
}
117+
}
118+
119+
/**
120+
* @return bool
121+
* @throws Exception
122+
* @throws \Tartan\Larapay\Adapter\Exception
123+
*/
124+
protected function verifyTransaction(): bool
125+
{
126+
127+
if ($this->getTransaction()->checkForVerify() == false) {
128+
throw new Exception('larapay::larapay.could_not_verify_payment');
129+
}
130+
131+
$this->checkRequiredParameters([
132+
'status',
133+
'trackid',
134+
]);
135+
136+
$sendParams = [
137+
'merchant' => $this->getSandbox(),
138+
'trackId' => $this->trackid,
139+
];
140+
141+
142+
try {
143+
XLog::debug('PaymentRequest call', $sendParams);
144+
$result = Helper::post2https($sendParams, $this->endPointVerify);
145+
$resultObj = json_decode($result);
146+
147+
XLog::info('PaymentRequest response', $this->obj2array($resultObj));
148+
if (isset($resultObj->result)) {
149+
if ($resultObj->result == 100 || $resultObj->result == 201) {
150+
$this->getTransaction()->setVerified();
151+
$this->getTransaction()->setReferenceId((string)$this->trackId);
152+
return true;
153+
} else {
154+
throw new Exception($resultObj->result);
155+
}
156+
} else {
157+
throw new Exception('larapay::larapay.invalid_response');
158+
}
159+
} catch (\Exception $e) {
160+
throw new Exception($e->getMessage());
161+
}
162+
163+
}
164+
165+
/**
166+
* @return bool
167+
*/
168+
public function canContinueWithCallbackParameters(): bool
169+
{
170+
171+
if ($this->success == 1) {
172+
return true;
173+
}
174+
175+
return false;
176+
}
177+
178+
public function getGatewayReferenceId(): string
179+
{
180+
$test = (array)$this->checkRequiredParameters([
181+
'trackid',
182+
]);
183+
return strval($this->trackid);
184+
185+
}
186+
}

0 commit comments

Comments
 (0)