Skip to content

Commit 8d93eb1

Browse files
committed
Idpay gateway added
1 parent d601b94 commit 8d93eb1

File tree

7 files changed

+338
-3
lines changed

7 files changed

+338
-3
lines changed

config/larapay.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| the gateways list is comma separated
2828
|
2929
*/
30-
'gateways' => env('LARAPAY_GATES', 'Mellat,Saman,Pasargad,Parsian,ZarinPal,Payir,Saderat'),
30+
'gateways' => env('LARAPAY_GATES', 'Mellat,Saman,Pasargad,Parsian,ZarinPal,Idpay,Payir,Saderat'),
3131

3232
/*
3333
|--------------------------------------------------------------------------
@@ -121,6 +121,24 @@
121121
'payir' => [
122122
'api' => env('PAY_IR_API_KEY', ''),
123123
],
124+
125+
/*
126+
|--------------------------------------------------------------------------
127+
| Idpay gateway configuration
128+
|--------------------------------------------------------------------------
129+
|
130+
| types: acceptable values --- normal
131+
|
132+
*/
133+
'idpay' => [
134+
'merchant_id' => env('IDPAY_MERCHANT_ID', ''),
135+
'type' => env('IDPAY_TYPE', 'normal'),
136+
'callback_url' => env('IDPAY_CALLBACK_URL', ''),
137+
'email' => env('IDPAY_EMAIL', ''),
138+
'mobile' => env('IDPAY_MOBILE', '09xxxxxxxxx'),
139+
'description' => env('IDPAY_DESCRIPTION', 'powered-by-Larapay'),
140+
],
141+
124142
/*
125143
|--------------------------------------------------------------------------
126144
| SoapClient Options

src/Adapter/Idpay.php

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tartan\Larapay\Adapter;
5+
6+
use Tartan\Larapay\Adapter\Idpay\Exception;
7+
use Tartan\Log\Facades\XLog;
8+
9+
10+
/**
11+
* Class Idpay
12+
* @package Tartan\Larapay\Adapter
13+
*/
14+
class Idpay extends AdapterAbstract implements AdapterInterface
15+
{
16+
protected $WSDL = 'https://api.idpay.ir/v1.1/payment';
17+
protected $endPoint = 'https://idpay.ir/p/ws/{order-id}';
18+
19+
protected $testWSDL = 'https://api.idpay.ir/v1.1/payment';
20+
protected $testEndPoint = 'https://idpay.ir/p/ws-sandbox/{order-id}';
21+
22+
public $endPointVerify = 'https://api.idpay.ir/v1.1/payment/verify';
23+
24+
public $reverseSupport = false;
25+
26+
/**
27+
* @return string
28+
* @throws Exception
29+
* @throws \Tartan\Larapay\Adapter\Exception
30+
*/
31+
protected function requestToken(): string
32+
{
33+
if ($this->getTransaction()->checkForRequestToken() == false) {
34+
throw new Exception('larapay::larapay.could_not_request_payment');
35+
}
36+
37+
$this->checkRequiredParameters([
38+
'order_id',
39+
'amount',
40+
'redirect_url',
41+
]);
42+
43+
$sendParams = [
44+
'order_id' => $this->getTransaction()->bank_order_id,
45+
'amount' => intval($this->amount),
46+
'desc' => $this->description ? $this->description : '',
47+
'mail' => $this->email ? $this->email : '',
48+
'phone' => $this->mobile ? $this->mobile : '',
49+
'callback' => $this->redirect_url,
50+
];
51+
52+
$header = [
53+
'Content-Type: application/json',
54+
'X-API-KEY:' .$this->merchant_id,
55+
'X-SANDBOX:' .$this->getSandbox()
56+
];
57+
try {
58+
XLog::debug('PaymentRequest call', $sendParams);
59+
$ch = curl_init();
60+
curl_setopt($ch, CURLOPT_URL, $this->WSDL);
61+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendParams));
62+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
63+
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
64+
$response = curl_exec($ch);
65+
$ch_error = curl_error($ch);
66+
curl_close($ch);
67+
$result = json_decode($response);
68+
69+
if (isset($result->error_code)) {
70+
throw new Exception($result->error_code);
71+
}
72+
73+
XLog::info('PaymentRequest response', $this->obj2array($result));
74+
$this->getTransaction()->setGatewayToken(strval($result->id)); // update transaction reference id
75+
return $result->id;
76+
} catch(\Exception $e) {
77+
throw new Exception($e->getMessage());
78+
};
79+
}
80+
81+
82+
/**
83+
* @return string
84+
* @throws Exception
85+
* @throws \Tartan\Larapay\Adapter\Exception
86+
*/
87+
protected function generateForm(): string
88+
{
89+
$authority = $this->requestToken();
90+
91+
$form = view('larapay::idpay-form', [
92+
'endPoint' => strtr($this->getEndPoint(), ['{order-id}' => $authority]),
93+
'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"),
94+
'autoSubmit' => boolval($this->auto_submit),
95+
]);
96+
97+
return $form->__toString();
98+
}
99+
100+
/**
101+
* @return array
102+
* @throws Exception
103+
* @throws \Tartan\Larapay\Adapter\Exception
104+
*/
105+
public function formParams(): array
106+
{
107+
$authority = $this->requestToken();
108+
109+
return [
110+
'endPoint' => strtr($this->getEndPoint(), ['{authority}' => $authority]),
111+
];
112+
}
113+
114+
public function getSandbox(): string
115+
{
116+
if (config('larapay.mode') == 'production') {
117+
return "0";
118+
} else {
119+
return "1";
120+
}
121+
}
122+
123+
/**
124+
* @return bool
125+
* @throws Exception
126+
* @throws \Tartan\Larapay\Adapter\Exception
127+
*/
128+
protected function verifyTransaction(): bool
129+
{
130+
131+
if ($this->getTransaction()->checkForVerify() == false) {
132+
throw new Exception('larapay::larapay.could_not_verify_payment');
133+
}
134+
135+
$this->checkRequiredParameters([
136+
'merchant_id',
137+
]);
138+
139+
$sendParams = [
140+
'id' => $this->getTransaction()->gate_refid,
141+
'order_id' => $this->getTransaction()->bank_order_id,
142+
];
143+
144+
$header = [
145+
'Content-Type: application/json',
146+
'X-API-KEY:' .$this->merchant_id,
147+
'X-SANDBOX:' .$this->getSandbox()
148+
];
149+
150+
try {
151+
$ch = curl_init();
152+
curl_setopt($ch, CURLOPT_URL, $this->endPointVerify);
153+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendParams));
154+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
155+
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
156+
$response = curl_exec($ch);
157+
$ch_error = curl_error($ch);
158+
curl_close($ch);
159+
XLog::debug('PaymentVerification call', $sendParams);
160+
$result = json_decode($response);
161+
XLog::info('PaymentVerification response', $this->obj2array($result));
162+
163+
if (isset($result->status)) {
164+
165+
if ($result->status == 100 || $result->status == 101) {
166+
$this->getTransaction()->setVerified();
167+
$this->getTransaction()->setReferenceId((string)$result->id);
168+
return true;
169+
} else {
170+
throw new Exception($result->status);
171+
}
172+
} else {
173+
throw new Exception($result->error_code);
174+
}
175+
176+
} catch(\Exception $e) {
177+
throw new Exception($e->getMessage());
178+
}
179+
180+
// try {
181+
// $soapClient = new SoapClient($this->getWSDL());
182+
//
183+
// XLog::debug('PaymentVerification call', $sendParams);
184+
//
185+
// $response = $soapClient->PaymentVerification($sendParams);
186+
//
187+
// XLog::info('PaymentVerification response', $this->obj2array($response));
188+
//
189+
//
190+
// if (isset($response->Status, $response->RefID)) {
191+
//
192+
// if ($response->Status == 100) {
193+
// $this->getTransaction()->setVerified();
194+
// $this->getTransaction()->setReferenceId((string)$response->RefID); // update transaction reference id
195+
//
196+
// return true;
197+
// } else {
198+
// throw new Exception($response->Status);
199+
// }
200+
// } else {
201+
// throw new Exception('larapay::larapay.invalid_response');
202+
// }
203+
//
204+
// } catch (SoapFault $e) {
205+
//
206+
// throw new Exception('SoapFault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
207+
// }
208+
}
209+
210+
/**
211+
* @return bool
212+
*/
213+
public function canContinueWithCallbackParameters(): bool
214+
{
215+
if (!empty($this->transaction['gate_refid'])) {
216+
return true;
217+
}
218+
219+
return false;
220+
}
221+
222+
public function getGatewayReferenceId(): string
223+
{
224+
$this->checkRequiredParameters([
225+
'merchant_id',
226+
]);
227+
228+
return strval($this->transaction['gate_refid']);
229+
}
230+
}

src/Adapter/Idpay/Exception.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tartan\Larapay\Adapter\Idpay;
4+
5+
class Exception extends \Tartan\Larapay\Adapter\Exception {
6+
protected $adapter = 'idpay';
7+
}

src/Models/Enum/Bank.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ class Bank
1313
const PAYIR = 'PayIr';
1414
const SADERAT = 'Saderat';
1515
const ZARINPAL = 'Zarinpal';
16+
const IDPAY = 'Idpay';
17+
1618
}

translations/en/larapay.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,37 @@
133133
'error__42 '=> 'مدت زمان معتبر طول عمر شناسه پرداخت باید بین ۳۰ دقیقه تا ۴۵ روز باشد',
134134
'error__54 '=> 'درخواست مورد نظر آرشیو شده است',
135135
]
136-
]
136+
],
137+
'idpay' => [
138+
'errors' => [
139+
"error_1" => "پرداخت انجام نشده است.",
140+
"error_2" => "پرداخت ناموفق بوده است.",
141+
"error_3" => "خطا رخ داده است.",
142+
"error_4" => "بلوکه شده.",
143+
"error_5" => "برگشت به پرداخت کننده.",
144+
"error_6" => "برگشت خورده سیستمی.",
145+
"error_10" => "در انتظار تایید پرداخت.",
146+
"error_100" => "پرداخت تایید شده است.",
147+
"error_101" => "پرداخت قبلا تایید شده است.",
148+
"error_200" => "به دریافت کننده واریز شد.",
149+
"error_11" => "کاربر مسدود شده است.",
150+
"error_12" => "API Key یافت نشد.",
151+
"error_13" => "درخواست شما از {ip} ارسال شده است. این IP با IP های ثبت شده در وب سرویس همخوانی ندارد.",
152+
"error_14" => "وب سرویس تایید نشده است.",
153+
"error_21" => "حساب بانکی متصل به وب سرویس تایید نشده است.",
154+
"error_31" => "کد تراکنش id نباید خالی باشد.",
155+
"error_32" => "شماره سفارش order_id نباید خالی باشد.",
156+
"error_33" => "مبلغ amount نباید خالی باشد.",
157+
"error_34" => "مبلغ amount باید بیشتر از {min-amount} ریال باشد.",
158+
"error_35" => "مبلغ amount باید کمتر از {max-amount} ریال باشد.",
159+
"error_36" => "مبلغ amount بیشتر از حد مجاز است.",
160+
"error_37" => "آدرس بازگشت callback نباید خالی باشد.",
161+
"error_38" => "درخواست شما از آدرس {domain} ارسال شده است. دامنه آدرس بازگشت callback با آدرس ثبت شده در وب سرویس همخوانی ندارد.",
162+
"error_51" => "تراکنش ایجاد نشد.",
163+
"error_52" => "استعلام نتیجه ای نداشت.",
164+
"error_53" => "تایید پرداخت امکان پذیر نیست.",
165+
"error_54" => "مدت زمان تایید پرداخت سپری شده است.",
166+
]
167+
]
168+
137169
];

translations/fa/larapay.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,37 @@
133133
'error__42 '=> 'مدت زمان معتبر طول عمر شناسه پرداخت باید بین ۳۰ دقیقه تا ۴۵ روز باشد',
134134
'error__54 '=> 'درخواست مورد نظر آرشیو شده است',
135135
]
136-
]
136+
],
137+
'idpay' => [
138+
'errors' => [
139+
"error_1" => "پرداخت انجام نشده است.",
140+
"error_2" => "پرداخت ناموفق بوده است.",
141+
"error_3" => "خطا رخ داده است.",
142+
"error_4" => "بلوکه شده.",
143+
"error_5" => "برگشت به پرداخت کننده.",
144+
"error_6" => "برگشت خورده سیستمی.",
145+
"error_10" => "در انتظار تایید پرداخت.",
146+
"error_100" => "پرداخت تایید شده است.",
147+
"error_101" => "پرداخت قبلا تایید شده است.",
148+
"error_200" => "به دریافت کننده واریز شد.",
149+
"error_11" => "کاربر مسدود شده است.",
150+
"error_12" => "API Key یافت نشد.",
151+
"error_13" => "درخواست شما از {ip} ارسال شده است. این IP با IP های ثبت شده در وب سرویس همخوانی ندارد.",
152+
"error_14" => "وب سرویس تایید نشده است.",
153+
"error_21" => "حساب بانکی متصل به وب سرویس تایید نشده است.",
154+
"error_31" => "کد تراکنش id نباید خالی باشد.",
155+
"error_32" => "شماره سفارش order_id نباید خالی باشد.",
156+
"error_33" => "مبلغ amount نباید خالی باشد.",
157+
"error_34" => "مبلغ amount باید بیشتر از {min-amount} ریال باشد.",
158+
"error_35" => "مبلغ amount باید کمتر از {max-amount} ریال باشد.",
159+
"error_36" => "مبلغ amount بیشتر از حد مجاز است.",
160+
"error_37" => "آدرس بازگشت callback نباید خالی باشد.",
161+
"error_38" => "درخواست شما از آدرس {domain} ارسال شده است. دامنه آدرس بازگشت callback با آدرس ثبت شده در وب سرویس همخوانی ندارد.",
162+
"error_51" => "تراکنش ایجاد نشد.",
163+
"error_52" => "استعلام نتیجه ای نداشت.",
164+
"error_53" => "تایید پرداخت امکان پذیر نیست.",
165+
"error_54" => "مدت زمان تایید پرداخت سپری شده است.",
166+
]
167+
]
168+
137169
];

views/idpay-form.blade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<form id="goto_idpay_bank" class="form-horizontal goto-bank-form" method="GET" action="{!! $endPoint !!}">
2+
<div class="control-group">
3+
<div class="controls">
4+
<button type="submit" class="btn btn-success">{{$submitLabel}}</button>
5+
</div>
6+
</div>
7+
</form>
8+
9+
@if($autoSubmit === true)
10+
<script type="text/javascript">
11+
var f=document.getElementById('goto_idpay_bank');
12+
f.submit();
13+
</script>
14+
@endif

0 commit comments

Comments
 (0)