Passed
Pull Request — master (#142)
by Hashem
08:43
created

Sizpay::purchase()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 28
rs 9.6
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(),
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
            throw new PurchaseFailedException($result->Message);
82
        }
83
84
        $this->invoice->transactionId($result->Token);
85
86
        // return the transaction's id
87
        return $this->invoice->getTransactionId();
88
    }
89
90
    /**
91
     * Pay the Invoice
92
     *
93
     * @return RedirectionForm
94
     */
95
    public function pay() : RedirectionForm
96
    {
97
        $payUrl = $this->settings->apiPaymentUrl;
98
99
        return $this->redirectWithForm(
100
            $payUrl,
101
            [
102
                'MerchantID' => $this->settings->merchantId,
103
                'TerminalID' => $this->settings->terminal,
104
                'Token' => $this->invoice->getTransactionId(),
105
                'SignData' => $this->settings->SignData
106
            ],
107
            'POST'
108
        );
109
    }
110
111
    /**
112
     * Verify payment
113
     *
114
     * @return ReceiptInterface
115
     *
116
     * @throws InvalidPaymentException
117
     * @throws \GuzzleHttp\Exception\GuzzleException
118
     */
119
    public function verify() : ReceiptInterface
120
    {
121
        $resCode = $this->invoice->getTransactionId() ?? Request::input('ResCod');
122
        if (! isset($resCode) || !in_array($resCode, array('0', '00'))) {
123
            $message = 'پرداخت توسط کاربر لغو شد';
124
            throw new InvalidPaymentException($message);
125
        }
126
127
        $data = array(
128
            'MerchantID'  => $this->settings->merchantId,
129
            'TerminalID'  => $this->settings->terminal,
130
            'UserName'    => $this->settings->username,
131
            'Password'    => $this->settings->password,
132
            'Token'       => Request::input('Token'),
133
            'SignData'    => $this->settings->SignData
134
        );
135
136
        $client = $this->client->request('GET', $this->settings->apiVerificationUrl);
137
        $response = $client->Confirm2($data)->Confirm2Result;
0 ignored issues
show
Bug introduced by
The method Confirm2() does not exist on Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
        $response = $client->/** @scrutinizer ignore-call */ Confirm2($data)->Confirm2Result;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
        $result = json_decode($response);
139
140
        if (! isset($result->ResCod) || ! in_array($result->ResCod, array('0', '00')))
141
        {
142
            $message = $result->Message ?? 'خطا در انجام عملیات رخ داده است';
143
            throw new InvalidPaymentException($message);
144
        }
145
146
        return $this->createReceipt($resCode);
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