Passed
Pull Request — master (#132)
by
unknown
02:18
created

Jibit::verify()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
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 19
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
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
    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, $this->settings->tokenStoragePath);
30
    }
31
32
33
    public function purchase()
34
    {
35
        $requestResult = $this->jibit->paymentRequest($this->invoice->getAmount(), $this->invoice->getUuid(true), $this->invoice->getDetail('mobile'), $this->settings->callbackUrl);
36
37
38
        if (!empty($requestResult['pspSwitchingUrl'])) {
39
            $this->payment_url = $requestResult['pspSwitchingUrl'];
40
        }
41
        if (!empty($requestResult['errors'])) {
42
            //fail result and show the error
43
            $errMsgs = array_map(function ($err) {
44
                return $err['message'];
45
            }, $requestResult['errors']);
46
            throw new PurchaseFailedException(implode('\n', $errMsgs));
47
        }
48
49
        $transId = $requestResult['orderIdentifier'];
50
        $referenceNumber = $requestResult['referenceNumber'];
51
        $this->invoice->detail('referenceNumber', $referenceNumber);
52
53
        $this->invoice->transactionId($transId);
54
55
        return $transId;
56
    }
57
58
    // Redirect into bank using transactionId, to complete the payment.
59
    public function pay() : RedirectionForm
60
    {
61
62
        // Redirect to the bank.
63
        $url = $this->payment_url;
64
        $inputs = [];
65
        $method = 'GET';
66
67
        return $this->redirectWithForm($url, $inputs, $method);
68
    }
69
70
    public function verify(): ReceiptInterface
71
    {
72
73
        // $verifyUrl = $this->settings->verifyApiUrl;
74
75
        $refNum = $this->invoice->getTransactionId();
76
        // Making payment verify
77
        $requestResult = $this->jibit->paymentVerify($refNum);
78
79
        if (!empty($requestResult['status']) && $requestResult['status'] === 'Successful') {
80
            //successful result
81
82
            //show session detail
83
            $order = $this->jibit->getOrderById($refNum);
84
85
            return (new Receipt('jibit', $refNum))->detail('payerCard', $order['payerCard'] ?? '');
86
        }
87
88
        throw new InvalidPaymentException('Payment failed.');
89
    }
90
}
91