1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Yekpay; |
4
|
|
|
|
5
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
6
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
7
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
8
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
9
|
|
|
use Shetabit\Multipay\Invoice; |
10
|
|
|
use Shetabit\Multipay\Receipt; |
11
|
|
|
use Shetabit\Multipay\RedirectionForm; |
12
|
|
|
use Shetabit\Multipay\Request; |
13
|
|
|
use SoapClient; |
14
|
|
|
use stdClass; |
15
|
|
|
|
16
|
|
|
class Yekpay extends Driver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Invoice |
20
|
|
|
* |
21
|
|
|
* @var Invoice |
22
|
|
|
*/ |
23
|
|
|
protected $invoice; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Driver settings |
27
|
|
|
* |
28
|
|
|
* @var object |
29
|
|
|
*/ |
30
|
|
|
protected $settings; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Yekpay constructor. |
34
|
|
|
* Construct the class with the relevant settings. |
35
|
|
|
* |
36
|
|
|
* @param Invoice $invoice |
37
|
|
|
* @param $settings |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Invoice $invoice, $settings) |
40
|
|
|
{ |
41
|
|
|
$this->invoice($invoice); |
42
|
|
|
$this->settings = (object) $settings; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve data from details using its name. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
private function extractDetails($name) |
51
|
|
|
{ |
52
|
|
|
return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Purchase Invoice. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
* |
60
|
|
|
* @throws PurchaseFailedException |
61
|
|
|
* @throws \SoapFault |
62
|
|
|
*/ |
63
|
|
|
public function purchase() |
64
|
|
|
{ |
65
|
|
|
$client = new SoapClient($this->settings->apiPurchaseUrl, array('trace' => true)); |
66
|
|
|
|
67
|
|
|
$data = new stdClass(); |
68
|
|
|
|
69
|
|
|
if (!empty($this->invoice->getDetails()['description'])) { |
70
|
|
|
$description = $this->invoice->getDetails()['description']; |
71
|
|
|
} else { |
72
|
|
|
$description = $this->settings->description; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$data->merchantId = $this->settings->merchantId; |
76
|
|
|
$data->amount = $this->invoice->getAmount(); |
77
|
|
|
$data->callback = $this->settings->callbackUrl; |
78
|
|
|
$data->orderNumber = intval(1, time()).crc32($this->invoice->getUuid()); |
79
|
|
|
|
80
|
|
|
$data->fromCurrencyCode = (int) $this->settings->fromCurrencyCode; |
81
|
|
|
$data->toCurrencyCode = (int) $this->settings->toCurrencyCode; |
82
|
|
|
|
83
|
|
|
$data->firstName = $this->extractDetails('firstName'); |
84
|
|
|
$data->lastName = $this->extractDetails('lastName'); |
85
|
|
|
$data->email = $this->extractDetails('email'); |
86
|
|
|
$data->mobile = $this->extractDetails('mobile'); |
87
|
|
|
|
88
|
|
|
$data->address = $this->extractDetails('address'); |
89
|
|
|
$data->country = $this->extractDetails('country'); |
90
|
|
|
$data->postalCode = $this->extractDetails('postalCode'); |
91
|
|
|
$data->city = $this->extractDetails('city'); |
92
|
|
|
|
93
|
|
|
$data->description = $description; |
94
|
|
|
|
95
|
|
|
$response = json_decode($client->request($data)); |
96
|
|
|
|
97
|
|
|
if ($response->Code != 100) { |
98
|
|
|
//"Request failed with Error code: $response->Code and Error message: $response->Description"; |
99
|
|
|
throw new PurchaseFailedException($response->Description); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->invoice->transactionId($response->Authority); |
103
|
|
|
|
104
|
|
|
// return the transaction's id |
105
|
|
|
return $this->invoice->getTransactionId(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Pay the Invoice |
110
|
|
|
* |
111
|
|
|
* @return RedirectionForm |
112
|
|
|
*/ |
113
|
|
|
public function pay() : RedirectionForm |
114
|
|
|
{ |
115
|
|
|
$payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId(); |
116
|
|
|
|
117
|
|
|
return $this->redirectWithForm($payUrl, [], 'GET'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Verify payment |
122
|
|
|
* |
123
|
|
|
* @return mixed|void |
124
|
|
|
* |
125
|
|
|
* @throws InvalidPaymentException |
126
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
127
|
|
|
*/ |
128
|
|
|
public function verify() : ReceiptInterface |
129
|
|
|
{ |
130
|
|
|
$options = array('trace' => true); |
131
|
|
|
$client = new SoapClient($this->settings->apiVerificationUrl, $options); |
132
|
|
|
|
133
|
|
|
$data = new stdClass(); |
134
|
|
|
|
135
|
|
|
$data->merchantId = $this->settings->merchantId; |
136
|
|
|
$data->authority = $this->invoice->getTransactionId() ?? Request::input('authority'); |
137
|
|
|
|
138
|
|
|
$response = json_decode($client->verify($data)); |
139
|
|
|
|
140
|
|
|
if ($response->Code != 100) { |
141
|
|
|
$this->notVerified($response->message ?? 'payment failed', $response->Code); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
//"Success Payment with reference: $response->Reference and message: $response->message"; |
145
|
|
|
return $this->createReceipt($response->Reference); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Generate the payment's receipt |
150
|
|
|
* |
151
|
|
|
* @param $referenceId |
152
|
|
|
* |
153
|
|
|
* @return ReceiptInterface |
154
|
|
|
*/ |
155
|
|
|
protected function createReceipt($referenceId) : ReceiptInterface |
156
|
|
|
{ |
157
|
|
|
$receipt = new Receipt('yekpay', $referenceId); |
158
|
|
|
|
159
|
|
|
return $receipt; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Trigger an exception |
164
|
|
|
* |
165
|
|
|
* @param $message |
166
|
|
|
* @param $status |
167
|
|
|
* |
168
|
|
|
* @throws InvalidPaymentException |
169
|
|
|
*/ |
170
|
|
|
private function notVerified($message, $status) |
171
|
|
|
{ |
172
|
|
|
if ($message) { |
173
|
|
|
throw new InvalidPaymentException($message, (int)$status); |
174
|
|
|
} else { |
175
|
|
|
throw new InvalidPaymentException('payment failed', (int)$status); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|