Skip to content

Commit 7521291

Browse files
PayIr Adapter
1 parent 98c8560 commit 7521291

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

config/larapay.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
'description' => env('ZARINPAL_MOBILE', 'powered-by-TartanPayment'),
112112
],
113113

114+
'pay_ir' => [
115+
'api' => env('PAY_IR_API_KEY', ''),
116+
],
114117
/*
115118
|--------------------------------------------------------------------------
116119
| SoapClient Options

src/Adapter/PayIr/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\PayIr;
4+
5+
class Exception extends \Tartan\Larapay\Adapter\Exception {
6+
protected $adapter = 'pay_ir';
7+
}

src/Adapter/Payir.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
namespace Tartan\Larapay\Adapter;
3+
4+
use Tartan\Larapay\Adapter\Zarinpal\Exception;
5+
use Illuminate\Support\Facades\Log;
6+
use Tartan\Larapay\Pasargad\Helper;
7+
8+
class Payir extends AdapterAbstract implements AdapterInterface
9+
{
10+
11+
public $endPoint = 'https://pay.ir/payment/send';
12+
public $endPointForm = 'https://pay.ir/payment/gateway/';
13+
public $endPointVerify = 'https://pay.ir/payment/verify';
14+
15+
public $reverseSupport = false;
16+
17+
/**
18+
* @return array
19+
* @throws Exception
20+
*/
21+
protected function requestToken ()
22+
{
23+
if ($this->getTransaction()->checkForRequestToken() == false) {
24+
throw new Exception('larapay::larapay.could_not_request_payment');
25+
}
26+
27+
$this->checkRequiredParameters([
28+
'api',
29+
'amount',
30+
'redirect_url',
31+
'order_id',
32+
]);
33+
34+
$sendParams = [
35+
'api' => $this->api,
36+
'amount' => intval($this->amount),
37+
'factorNumber' => ($this->order_id),
38+
'description' => $this->description ? $this->description : '',
39+
'mobile' => $this->mobile ? $this->mobile : '',
40+
'redirect' => $this->redirect_url,
41+
];
42+
43+
try {
44+
45+
46+
47+
Log::debug('PaymentRequest call', $sendParams);
48+
$result = Helper::post2https($sendParams , $this->endPoint);
49+
50+
51+
$resultobj = json_decode($result);
52+
Log::info('PaymentRequest response', $this->obj2array($resultobj));
53+
54+
55+
if (isset($resultobj->status)) {
56+
57+
if ($resultobj->status == 1) {
58+
$this->getTransaction()->setReferenceId($resultobj->transId); // update transaction reference id
59+
return $resultobj->transId;
60+
}
61+
else {
62+
throw new Exception($resultobj->status);
63+
}
64+
}
65+
else {
66+
throw new Exception('larapay::larapay.invalid_response');
67+
}
68+
} catch (\Exception $e) {
69+
throw new Exception('PayIr Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
70+
}
71+
}
72+
73+
74+
/**
75+
* @return mixed
76+
*/
77+
protected function generateForm ()
78+
{
79+
$authority = $this->requestToken();
80+
return view('larapay::payir-form', [
81+
'endPoint' => $this->endPointForm.$authority,
82+
'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"),
83+
'autoSubmit' => true
84+
]);
85+
}
86+
87+
/**
88+
* @return bool
89+
* @throws Exception
90+
*/
91+
protected function verifyTransaction ()
92+
{
93+
if($this->getTransaction()->checkForVerify() == false) {
94+
throw new Exception('larapay::larapay.could_not_verify_payment');
95+
}
96+
97+
$this->checkRequiredParameters([
98+
'api',
99+
'transId',
100+
]);
101+
102+
$sendParams = [
103+
'api' => $this->api,
104+
'transId' => $this->transId,
105+
];
106+
107+
try {
108+
109+
Log::debug('PaymentVerification call', $sendParams);
110+
$result = Helper::post2https($sendParams , $this->endPointVerify);
111+
$response = json_decode($result);
112+
Log::info('PaymentVerification response', $this->obj2array($response));
113+
114+
115+
if (isset($response->status, $response->amount)) {
116+
117+
if($response->status == 1) {
118+
$this->getTransaction()->setVerified();
119+
$this->getTransaction()->setReferenceId($this->transId); // update transaction reference id
120+
return true;
121+
} else {
122+
throw new Exception($response->status);
123+
}
124+
} else {
125+
throw new Exception('larapay::larapay.invalid_response');
126+
}
127+
128+
} catch (\Exception $e) {
129+
130+
throw new Exception('PayIr: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
131+
}
132+
}
133+
134+
/**
135+
* @return bool
136+
*/
137+
public function canContinueWithCallbackParameters()
138+
{
139+
140+
if (!empty($this->parameters['transId'])) {
141+
return true;
142+
}
143+
return false;
144+
}
145+
146+
public function getGatewayReferenceId()
147+
{
148+
$this->checkRequiredParameters([
149+
'transId',
150+
]);
151+
return $this->transId;
152+
}
153+
}

views/payir-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_payir_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_payir_bank');
12+
f.submit();
13+
</script>
14+
@endif

0 commit comments

Comments
 (0)