1
|
|
|
<?php |
2
|
|
|
namespace Shetabit\Multipay\Drivers\Jibit; |
3
|
|
|
|
4
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
5
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
6
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
7
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
8
|
|
|
use Shetabit\Multipay\Invoice; |
9
|
|
|
use Shetabit\Multipay\RedirectionForm; |
10
|
|
|
use Shetabit\Multipay\Receipt; |
11
|
|
|
|
12
|
|
|
class Jibit extends Driver |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Jibit client |
16
|
|
|
* |
17
|
|
|
* @var JibitClient |
18
|
|
|
*/ |
19
|
|
|
protected $jibit; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Invoice |
23
|
|
|
* |
24
|
|
|
* @var Invoice |
25
|
|
|
*/ |
26
|
|
|
protected $invoice; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Driver settings |
30
|
|
|
* |
31
|
|
|
* @var object |
32
|
|
|
*/ |
33
|
|
|
protected $settings; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Payment URL |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $paymentUrl; |
41
|
|
|
|
42
|
|
|
public function __construct(Invoice $invoice, $settings) |
43
|
|
|
{ |
44
|
|
|
$this->invoice($invoice); |
45
|
|
|
$this->settings = (object) $settings; |
46
|
|
|
$this->jibit = new JibitClient( |
47
|
|
|
$this->settings->apiKey, |
48
|
|
|
$this->settings->apiSecret, |
49
|
|
|
$this->settings->apiPaymentUrl, |
50
|
|
|
$this->settings->tokenStoragePath |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Purchase invoice |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
* @throws PurchaseFailedException |
59
|
|
|
*/ |
60
|
|
|
public function purchase() |
61
|
|
|
{ |
62
|
|
|
$amount = $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1); // Convert to Rial |
63
|
|
|
|
64
|
|
|
$requestResult = $this->jibit->paymentRequest( |
65
|
|
|
$amount, |
|
|
|
|
66
|
|
|
$this->invoice->getUuid(), |
67
|
|
|
$this->invoice->getDetail('mobile'), |
68
|
|
|
$this->settings->callbackUrl |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
if (! empty($requestResult['pspSwitchingUrl'])) { |
73
|
|
|
$this->paymentUrl = $requestResult['pspSwitchingUrl']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (! empty($requestResult['errors'])) { |
77
|
|
|
$errMsgs = array_map(function ($err) { |
78
|
|
|
return $err['code']; |
79
|
|
|
}, $requestResult['errors']); |
80
|
|
|
|
81
|
|
|
throw new PurchaseFailedException(implode('\n', $errMsgs)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$purchaseId = $requestResult['purchaseId']; |
85
|
|
|
$referenceNumber = $requestResult['clientReferenceNumber']; |
86
|
|
|
|
87
|
|
|
$this->invoice->detail('referenceNumber', $referenceNumber); |
88
|
|
|
$this->invoice->transactionId($purchaseId); |
89
|
|
|
|
90
|
|
|
return $purchaseId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Pay invoice |
95
|
|
|
* |
96
|
|
|
* @return RedirectionForm |
97
|
|
|
*/ |
98
|
|
|
public function pay() : RedirectionForm |
99
|
|
|
{ |
100
|
|
|
$url = $this->paymentUrl; |
101
|
|
|
|
102
|
|
|
return $this->redirectWithForm($url, [], 'GET'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Verify payment |
107
|
|
|
* |
108
|
|
|
* @return ReceiptInterface |
109
|
|
|
* @throws InvalidPaymentException |
110
|
|
|
* @throws PurchaseFailedException |
111
|
|
|
*/ |
112
|
|
|
public function verify(): ReceiptInterface |
113
|
|
|
{ |
114
|
|
|
$purchaseId = $this->invoice->getTransactionId(); |
115
|
|
|
|
116
|
|
|
$requestResult = $this->jibit->paymentVerify($purchaseId); |
117
|
|
|
|
118
|
|
|
if (! empty($requestResult['status']) && $requestResult['status'] === 'SUCCESSFUL') { |
119
|
|
|
$order = $this->jibit->getOrderById($purchaseId); |
120
|
|
|
|
121
|
|
|
$receipt = new Receipt('jibit', $purchaseId); |
122
|
|
|
return $receipt->detail('payerCard', $order['elements']['payerCardNumber'] ?? ''); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
throw new InvalidPaymentException('Payment encountered an issue.'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|