Passed
Pull Request — master (#85)
by
unknown
07:19
created

Jibit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
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
    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
       // $verifyUrl = $this->settings->verifyApiUrl;
76
77
        $refNum = $this->invoice->getTransactionId();
78
        // Making payment verify
79
        $requestResult = $this->jibit->paymentVerify($refNum);
80
81
        if (!empty($requestResult['status']) && $requestResult['status'] === 'Successful') {
82
            //successful result
83
84
            //show session detail
85
            $order = $this->jibit->getOrderById($refNum);
86
            if (!empty($order['payerCard'])){
87
                //echo 'payer card pan mask: ' .$order['payerCard'];
88
            }
89
            //return new Receipt('jibit', 'payment_receipt_number');
90
            return new Receipt('jibit', $refNum);
91
92
        }
93
94
        throw new InvalidPaymentException('تراکنش ناموفق.');
95
96
97
    }
98
99
100
//    /**
101
//     * Prepare data for purchasing invoice
102
//     *
103
//     * @return array
104
//     */
105
//    protected function preparePurchaseData()
106
//    {
107
//        if (!empty($this->invoice->getDetails()['description'])) {
108
//            $description = $this->invoice->getDetails()['description'];
109
//        } else {
110
//            $description = $this->settings->description;
111
//        }
112
//
113
//        return array(
114
//            'LoginAccount'      => $this->settings->merchantId,
115
//            'Amount'            => $this->invoice->getAmount() * 10, // convert to rial
116
//            'OrderId'           => crc32($this->invoice->getUuid()),
117
//            'CallBackUrl'       => $this->settings->callbackUrl,
118
//            'AdditionalData'    => $description,
119
//        );
120
//    }
121
}
122