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

Jibit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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