Sizpay   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 146
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createReceipt() 0 5 1
A __construct() 0 5 1
A pay() 0 13 1
A purchase() 0 29 4
A verify() 0 27 5
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Sizpay;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Multipay\Abstracts\Driver;
7
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
8
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
9
use Shetabit\Multipay\Contracts\ReceiptInterface;
10
use Shetabit\Multipay\Invoice;
11
use Shetabit\Multipay\Receipt;
12
use Shetabit\Multipay\RedirectionForm;
13
use Shetabit\Multipay\Request;
14
15
class Sizpay extends Driver
16
{
17
    /**
18
     * Nextpay Client.
19
     *
20
     * @var Client
21
     */
22
    protected $client;
23
24
    /**
25
     * Invoice
26
     *
27
     * @var Invoice
28
     */
29
    protected $invoice;
30
31
    /**
32
     * Driver settings
33
     *
34
     * @var object
35
     */
36
    protected $settings;
37
38
    /**
39
     * Nextpay constructor.
40
     * Construct the class with the relevant settings.
41
     *
42
     * @param Invoice $invoice
43
     * @param $settings
44
     */
45
    public function __construct(Invoice $invoice, $settings)
46
    {
47
        $this->invoice($invoice);
48
        $this->settings = (object) $settings;
49
        $this->client = new Client();
50
    }
51
52
    /**
53
     * Purchase Invoice.
54
     *
55
     * @return string
56
     *
57
     * @throws PurchaseFailedException
58
     * @throws \SoapFault
59
     */
60
    public function purchase()
61
    {
62
        $client = new \SoapClient($this->settings->apiPurchaseUrl);
63
        $response = $client->GetToken2(array(
64
            'MerchantID' => $this->settings->merchantId,
65
            'TerminalID' => $this->settings->terminal,
66
            'UserName' => $this->settings->username,
67
            'Password' => $this->settings->password,
68
            'Amount' => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
69
            'OrderID' => time(),
70
            'ReturnURL' => $this->settings->callbackUrl,
71
            'InvoiceNo' => time(),
72
            'DocDate' => '',
73
            'ExtraInf' => time(),
74
            'AppExtraInf' => '',
75
            'SignData' => $this->settings->SignData
76
        ))->GetToken2Result;
77
        $result = json_decode($response);
78
79
        if (! isset($result->ResCod) || ! in_array($result->ResCod, ['0', '00'])) {
80
            // error has happened
81
            $message = $result->Message ?? 'خطای ناشناخته رخ داده';
82
            throw new PurchaseFailedException($message);
83
        }
84
85
        $this->invoice->transactionId($result->Token);
86
87
        // return the transaction's id
88
        return $this->invoice->getTransactionId();
89
    }
90
91
    /**
92
     * Pay the Invoice
93
     *
94
     * @return RedirectionForm
95
     */
96
    public function pay() : RedirectionForm
97
    {
98
        $payUrl = $this->settings->apiPaymentUrl;
99
100
        return $this->redirectWithForm(
101
            $payUrl,
102
            [
103
                'MerchantID' => $this->settings->merchantId,
104
                'TerminalID' => $this->settings->terminal,
105
                'Token' => $this->invoice->getTransactionId(),
106
                'SignData' => $this->settings->SignData
107
            ],
108
            'POST'
109
        );
110
    }
111
112
    /**
113
     * Verify payment
114
     *
115
     * @return ReceiptInterface
116
     *
117
     * @throws InvalidPaymentException
118
     * @throws \GuzzleHttp\Exception\GuzzleException
119
     */
120
    public function verify() : ReceiptInterface
121
    {
122
        $resCode = Request::input('ResCod');
123
        if (! in_array($resCode, array('0', '00'))) {
124
            $message = 'پرداخت توسط کاربر لغو شد';
125
            throw new InvalidPaymentException($message);
126
        }
127
128
        $data = array(
129
            'MerchantID'  => $this->settings->merchantId,
130
            'TerminalID'  => $this->settings->terminal,
131
            'UserName'    => $this->settings->username,
132
            'Password'    => $this->settings->password,
133
            'Token'       => Request::input('Token'),
134
            'SignData'    => $this->settings->SignData
135
        );
136
137
        $client = new \SoapClient($this->settings->apiVerificationUrl);
138
        $response = $client->Confirm2($data)->Confirm2Result;
139
        $result = json_decode($response);
140
141
        if (! isset($result->ResCod) || ! in_array($result->ResCod, array('0', '00'))) {
142
            $message = $result->Message ?? 'خطا در انجام عملیات رخ داده است';
143
            throw new InvalidPaymentException($message, (int)(isset($result->ResCod) ? $result->ResCod : 0));
144
        }
145
146
        return $this->createReceipt($result->RefNo);
147
    }
148
149
    /**
150
     * Generate the payment's receipt
151
     *
152
     * @param $resCode
153
     *
154
     * @return Receipt
155
     */
156
    protected function createReceipt($resCode)
157
    {
158
        $receipt = new Receipt('sizpay', $resCode);
159
160
        return $receipt;
161
    }
162
}
163