Passed
Pull Request — master (#142)
by Hashem
02:45
created

Sizpay::purchase()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 29
rs 9.584
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Sizpay;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Facades\Log;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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