Passed
Pull Request — master (#85)
by
unknown
03:35 queued 34s
created

Jibit::verify()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 3
eloc 6
c 2
b 0
f 2
nc 2
nop 0
dl 0
loc 21
rs 10
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
    Exceptions\PurchaseFailedException,
8
    Invoice,
9
    RedirectionForm,
10
    Receipt};
11
12
class Jibit extends Driver
13
{
14
    protected $invoice; // Invoice.
15
16
    protected $settings; // Driver settings.
17
18
    protected $payment_url;
19
    /**
20
     * @var JibitBase
21
     */
22
    protected $jibit; // Driver settings.
23
24
    public function __construct(Invoice $invoice, $settings)
25
    {
26
        $this->invoice($invoice); // Set the invoice.
27
        $this->settings = (object) $settings; // Set settings.
28
        /** @var JibitBase $jibit */
29
        $this->jibit = new JibitBase($this->settings->merchantId, $this->settings->apiSecret,$this->settings->apiPaymentUrl);
30
31
    }
32
33
    // Purchase the invoice, save its transactionId and finaly return it.
34
    public function purchase() {
35
        // Request for a payment transaction id.
36
37
        // Making payment request
38
        // you should save the order details in DB, you need if for verify
39
        $requestResult = $this->jibit->paymentRequest($this->invoice->getAmount(), $this->invoice->getDetail('order_id'), $this->invoice->getDetail('mobile'),$this->settings->callbackUrl);
40
41
42
        if (!empty($requestResult['pspSwitchingUrl'])) {
43
            //successful result and redirect to PG
44
            $this->payment_url = $requestResult['pspSwitchingUrl'];
45
46
        }
47
        if (!empty($requestResult['errors'])) {
48
            //fail result and show the error
49
            $errMsgs = array_map(function ($err){ return $err['message']; }, $requestResult['errors']);
50
            throw new PurchaseFailedException(implode('\n', $errMsgs));
51
        }
52
53
        $transId = $requestResult['orderIdentifier'];
54
        $referenceNumber = $requestResult['referenceNumber'];
0 ignored issues
show
Unused Code introduced by
The assignment to $referenceNumber is dead and can be removed.
Loading history...
55
56
        $this->invoice->transactionId($transId);
57
58
        return $transId;
59
    }
60
61
    // Redirect into bank using transactionId, to complete the payment.
62
    public function pay() : RedirectionForm {
63
64
        // Redirect to the bank.
65
        $url = $this->payment_url;
66
        $inputs = [];
67
        $method = 'GET';
68
69
        return $this->redirectWithForm($url, $inputs, $method);
70
    }
71
72
    // Verify the payment (we must verify to ensure that user has paid the invoice).
73
    public function verify(): ReceiptInterface
74
    {
75
76
        // $verifyUrl = $this->settings->verifyApiUrl;
77
78
        $refNum = $this->invoice->getTransactionId();
79
        // Making payment verify
80
        $requestResult = $this->jibit->paymentVerify($refNum);
81
82
        if (!empty($requestResult['status']) && $requestResult['status'] === 'Successful') {
83
            //successful result
84
85
            //show session detail
86
            $order = $this->jibit->getOrderById($refNum);
87
88
            return (new Receipt('jibit', $refNum))->detail('payerCard', $order['payerCard'] ?? '');
89
90
91
        }
92
93
        throw new InvalidPaymentException('Payment failed.');
94
95
96
    }
97
}
98