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($this->settings->apiKey, $this->settings->apiSecret, $this->settings->apiPaymentUrl, $this->settings->tokenStoragePath); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Purchase invoice |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @throws PurchaseFailedException |
54
|
|
|
*/ |
55
|
|
|
public function purchase() |
56
|
|
|
{ |
57
|
|
|
$requestResult = $this->jibit->paymentRequest($this->invoice->getAmount(), $this->invoice->getUuid(), $this->invoice->getDetail('mobile'), $this->settings->callbackUrl); |
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
if (! empty($requestResult['pspSwitchingUrl'])) { |
61
|
|
|
$this->paymentUrl = $requestResult['pspSwitchingUrl']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (! empty($requestResult['errors'])) { |
65
|
|
|
$errMsgs = array_map(function ($err) { |
66
|
|
|
return $err['code']; |
67
|
|
|
}, $requestResult['errors']); |
68
|
|
|
|
69
|
|
|
throw new PurchaseFailedException(implode('\n', $errMsgs)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$purchaseId = $requestResult['purchaseId']; |
73
|
|
|
$referenceNumber = $requestResult['clientReferenceNumber']; |
74
|
|
|
|
75
|
|
|
$this->invoice->detail('referenceNumber', $referenceNumber); |
76
|
|
|
$this->invoice->transactionId($purchaseId); |
77
|
|
|
|
78
|
|
|
return $purchaseId; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Pay invoice |
83
|
|
|
* |
84
|
|
|
* @return RedirectionForm |
85
|
|
|
*/ |
86
|
|
|
public function pay() : RedirectionForm |
87
|
|
|
{ |
88
|
|
|
$url = $this->paymentUrl; |
89
|
|
|
|
90
|
|
|
return $this->redirectWithForm($url, [], 'GET'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Verify payment |
95
|
|
|
* |
96
|
|
|
* @return ReceiptInterface |
97
|
|
|
* @throws InvalidPaymentException |
98
|
|
|
* @throws PurchaseFailedException |
99
|
|
|
*/ |
100
|
|
|
public function verify(): ReceiptInterface |
101
|
|
|
{ |
102
|
|
|
$purchaseId = $this->invoice->getTransactionId(); |
103
|
|
|
|
104
|
|
|
$requestResult = $this->jibit->paymentVerify($purchaseId); |
105
|
|
|
|
106
|
|
|
if (! empty($requestResult['status']) && $requestResult['status'] === 'SUCCESSFUL') { |
107
|
|
|
$order = $this->jibit->getOrderById($purchaseId); |
108
|
|
|
|
109
|
|
|
$receipt = new Receipt('jibit', $purchaseId); |
110
|
|
|
return $receipt->detail('payerCard', $order['elements']['payerCardNumber'] ?? ''); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new InvalidPaymentException('Payment encountered an issue.'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|