1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Toman; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Shetabit\Multipay\Invoice; |
7
|
|
|
use Shetabit\Multipay\Receipt; |
8
|
|
|
use Shetabit\Multipay\RedirectionForm; |
9
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
10
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
11
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
12
|
|
|
use Shetabit\Multipay\Request; |
13
|
|
|
|
14
|
|
|
class Toman extends Driver |
15
|
|
|
{ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
protected $invoice; // Invoice. |
19
|
|
|
|
20
|
|
|
protected $settings; // Driver settings. |
21
|
|
|
|
22
|
|
|
protected $base_url; |
23
|
|
|
|
24
|
|
|
protected $shop_slug; |
25
|
|
|
|
26
|
|
|
protected $auth_code; |
27
|
|
|
|
28
|
|
|
protected $code; |
29
|
|
|
|
30
|
|
|
protected $auth_token; |
31
|
|
|
|
32
|
|
|
public function __construct(Invoice $invoice, $settings) |
33
|
|
|
{ |
34
|
|
|
$this->invoice($invoice); // Set the invoice. |
35
|
|
|
$this->settings = (object) $settings; // Set settings. |
36
|
|
|
$this->base_url = $this->settings->base_url; |
37
|
|
|
$this->shop_slug = $this->settings->shop_slug; |
38
|
|
|
$this->auth_code = $this->settings->auth_code; |
39
|
|
|
$this->code = $this->shop_slug . ':' . $this->auth_code; |
40
|
|
|
$this->auth_token = base64_encode($this->code); |
41
|
|
|
$this->client = new Client(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Purchase the invoice, save its transactionId and finaly return it. |
45
|
|
|
public function purchase() |
46
|
|
|
{ |
47
|
|
|
$url = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals"; |
48
|
|
|
$data = $this->settings->data; |
49
|
|
|
|
50
|
|
|
$response = $this->client |
51
|
|
|
->request( |
52
|
|
|
'POST', |
53
|
|
|
$url, |
54
|
|
|
[ |
55
|
|
|
'json' => $data, |
56
|
|
|
'headers' => [ |
57
|
|
|
'Authorization' => "Basic {$this->auth_token}", |
58
|
|
|
"Content-Type" => 'application/json' |
59
|
|
|
] |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$result = json_decode($response->getBody()->getContents(), true); |
64
|
|
|
|
65
|
|
|
if (isset($result['trace_number'])) { |
66
|
|
|
$this->invoice->transactionId($result['trace_number']); |
67
|
|
|
return $this->invoice->getTransactionId(); |
68
|
|
|
} else { |
69
|
|
|
throw new InvalidPaymentException('پرداخت با مشکل مواجه شد، لطفا با ما در ارتباط باشید'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Redirect into bank using transactionId, to complete the payment. |
74
|
|
|
public function pay(): RedirectionForm |
75
|
|
|
{ |
76
|
|
|
$transactionId = $this->invoice->getTransactionId(); |
77
|
|
|
$redirect_url = $this->base_url . '/deals/' . $transactionId . '/redirect'; |
78
|
|
|
|
79
|
|
|
return $this->redirectWithForm($redirect_url, [], 'GET'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Verify the payment (we must verify to ensure that user has paid the invoice). |
83
|
|
|
public function verify(): ReceiptInterface |
84
|
|
|
{ |
85
|
|
|
$state = Request::input('state'); |
86
|
|
|
|
87
|
|
|
$transactionId = $this->invoice->getTransactionId(); |
88
|
|
|
$verifyUrl = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals/" . $transactionId . "/verify"; |
89
|
|
|
|
90
|
|
|
if ($state != 'funded') { |
91
|
|
|
throw new InvalidPaymentException('پرداخت انجام نشد'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->client |
95
|
|
|
->request( |
96
|
|
|
'PATCH', |
97
|
|
|
$verifyUrl, |
98
|
|
|
[ |
99
|
|
|
'headers' => [ |
100
|
|
|
'Authorization' => "Basic {$this->auth_token}", |
101
|
|
|
"Content-Type" => 'application/json' |
102
|
|
|
] |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
return $this->createReceipt($transactionId); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Generate the payment's receipt |
111
|
|
|
* |
112
|
|
|
* @param $referenceId |
113
|
|
|
* |
114
|
|
|
* @return Receipt |
115
|
|
|
*/ |
116
|
|
|
public function createReceipt($referenceId) |
117
|
|
|
{ |
118
|
|
|
return new Receipt('toman', $referenceId); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|