Completed
Push — master ( e1904b...37376c )
by mahdi
03:58
created

Irankish::verify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Shetabit\Payment\Drivers;
4
5
use Shetabit\Payment\Abstracts\Driver;
6
use Shetabit\Payment\Exceptions\InvalidPaymentException;
7
use Shetabit\Payment\Invoice;
8
9
class Irankish extends Driver
10
{
11
    /**
12
     * Invoice
13
     *
14
     * @var Invoice
15
     */
16
    protected $invoice;
17
18
    /**
19
     * Driver settings
20
     *
21
     * @var object
22
     */
23
    protected $settings;
24
25
    /**
26
     * Zarinpal constructor.
27
     * Construct the class with the relevant settings.
28
     *
29
     * @param Invoice $invoice
30
     * @param $settings
31
     */
32
    public function __construct(Invoice $invoice, $settings)
33
    {
34
        $this->invoice($invoice);
35
        $this->settings = (object) $settings;
36
    }
37
38
    /**
39
     * Purchase Invoice.
40
     *
41
     * @return string
42
     */
43
    public function purchase()
44
    {
45
        if (!empty($this->invoice->getDetails()['description'])) {
46
            $description = $this->invoice->getDetails()['description'];
47
        } else {
48
            $description = $this->settings->description;
49
        }
50
51
        $data = array(
52
            'amount' => $this->invoice->getAmount(),
53
            'merchantId' => $this->settings->merchantId,
54
            'description' => $description,
55
            'revertURL' => $this->settings->callbackUrl,
56
            'invoiceNo' => $this->invoice->getUuid(),
57
            'paymentId' => $this->invoice->getUuid(),
58
            'specialPaymentId' => $this->invoice->getUuid(),
59
        );
60
61
        $soap = new \SoapClient(
62
            $this->settings->apiPurchaseUrl,
63
            array('soap_version'   => SOAP_1_1)
64
        );
65
        $response = $soap->MakeToken($data);
66
67
        if ($response->MakeTokenResult->result != false) {
68
            $this->invoice->transactionId($response->MakeTokenResult->token);
69
        }
70
71
        // return the transaction's id
72
        return $this->invoice->getTransactionId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->invoice->getTransactionId() returns the type string which is incompatible with the return type mandated by Shetabit\Payment\Contrac...erInterface::purchase() of Shetabit\Payment\Contracts\DriverInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
73
    }
74
75
    /**
76
     * Pay the Invoice
77
     *
78
     * @return \Illuminate\Http\RedirectResponse|mixed
79
     */
80
    public function pay()
81
    {
82
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
83
84
        // redirect using laravel logic
85
        return redirect()->to($payUrl);
86
    }
87
88
    /**
89
     * Verify payment
90
     *
91
     * @return mixed|void
92
     * @throws InvalidPaymentException
93
     */
94
    public function verify()
95
    {
96
        $data = array(
97
            'token' => $this->invoice->getTransactionId(),
98
            'referenceNumber' => $this->invoice->getUuid(),
99
            'merchantId' => $this->settings->merchantId,
100
            'sha1Key' => $this->settings->sha1Key,
101
        );
102
103
        $soap = new \SoapClient($this->settings->apiVerificationUrl);
104
        $response = $soap->KicccPaymentsVerification($data);
105
106
        $status = intval($response->KicccPaymentsVerificationResult);
107
108
        if ($response != 100) {
109
            $this->notVerified($status);
110
        }
111
    }
112
113
    /**
114
     * Trigger an exception
115
     *
116
     * @param $status
117
     * @throws InvalidPaymentException
118
     */
119
    private function notVerified($status)
120
    {
121
        $translations = array(
122
            110 => 'دارنده کارت انصراف داده است',
123
            120 => 'موجودی حساب کافی نمی باشد',
124
            121 => 'مبلغ تراکنشهای کارت بیش از حد مجاز است',
125
            130 => 'اطلاعات کارت نادرست می باشد',
126
            131 => 'رمز کارت اشتباه است',
127
            132 => 'کارت مسدود است',
128
            133 => 'کارت منقضی شده است',
129
            140 => 'زمان مورد نظر به پایان رسیده است',
130
            150 => 'خطای داخلی بانک به وجود آمده است',
131
            160 => 'خطای انقضای کارت به وجود امده یا اطلاعات CVV2 اشتباه است',
132
            166 => 'بانک صادر کننده کارت شما مجوز انجام تراکنش را صادر نکرده است',
133
            167 => 'خطا در مبلغ تراکنش',
134
            200 => 'مبلغ تراکنش بیش از حدنصاب مجاز',
135
            201 => 'مبلغ تراکنش بیش از حدنصاب مجاز برای روز کاری',
136
            202 => 'مبلغ تراکنش بیش از حدنصاب مجاز برای ماه کاری',
137
            203 => 'تعداد تراکنشهای مجاز از حد نصاب گذشته است',
138
            499 => 'خطای سیستمی ، لطفا مجددا تالش فرمایید',
139
            500 => 'خطا در تایید تراکنش های خرد شده',
140
            501 => 'خطا در تایید تراکتش ، ویزگی تایید خودکار',
141
            502 => 'آدرس آی پی نا معتبر',
142
            503 => 'پذیرنده در حالت تستی می باشد ، مبلغ نمی تواند بیش از حد مجاز تایین شده برای پذیرنده تستی باشد',
143
            504 => 'خطا در بررسی الگوریتم شناسه پرداخت',
144
            505 => 'مدت زمان الزم برای انجام تراکنش تاییدیه به پایان رسیده است',
145
            506 => 'ذیرنده یافت نشد',
146
            507 => 'توکن نامعتبر/طول عمر توکن منقضی شده است',
147
            508 => 'توکن مورد نظر یافت نشد و یا منقضی شده است',
148
            509 => 'خطا در پارامترهای اجباری خرید تسهیم شده',
149
            510 => 'خطا در تعداد تسهیم | مبالغ کل تسهیم مغایر با مبلغ کل ارائه شده | خطای شماره ردیف تکراری',
150
            511 => 'حساب مسدود است',
151
            512 => 'حساب تعریف نشده است',
152
            513 => 'شماره تراکنش تکراری است',
153
            -20 => 'در درخواست کارکتر های غیر مجاز وجو دارد',
154
            -30 => 'تراکنش قبلا برگشت خورده است',
155
            -50 => 'طول رشته درخواست غیر مجاز است',
156
            -51 => 'در در خواست خطا وجود دارد',
157
            -80 => 'تراکنش مورد نظر یافت نشد',
158
            -81 => ' خطای داخلی بانک',
159
            -90 => 'تراکنش قبلا تایید شده است'
160
        );
161
        if (array_key_exists($status, $translations)) {
162
            throw new InvalidPaymentException($translations[$status]);
163
        } else {
164
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
165
        }
166
    }
167
}
168