Passed
Push — master ( 57f97b...129e30 )
by mahdi
03:11
created

Zarinpal::extractDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Shetabit\Payment\Drivers;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Payment\Abstracts\Driver;
7
use Shetabit\Payment\Exceptions\InvalidPaymentException;
8
use Shetabit\Payment\Invoice;
9
10
class Zarinpal extends Driver
11
{
12
    /**
13
     * Zarinpal Client.
14
     *
15
     * @var object
16
     */
17
    protected $client;
18
19
    /**
20
     * Invoice
21
     *
22
     * @var Invoice
23
     */
24
    protected $invoice;
25
26
    /**
27
     * Driver settings
28
     *
29
     * @var object
30
     */
31
    protected $settings;
32
33
    /**
34
     * Zarinpal constructor.
35
     * Construct the class with the relevant settings.
36
     *
37
     * @param Invoice $invoice
38
     * @param $settings
39
     */
40
    public function __construct(Invoice $invoice, $settings)
41
    {
42
        $this->invoice($invoice);
43
        $this->settings = (object) $settings;
44
        $this->client = new Client();
45
    }
46
47
    /**
48
     * Retrieve data from details using its name.
49
     *
50
     * @return string
51
     */
52
    private function extractDetails($name)
53
    {
54
    	return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name];
55
    }
56
57
    /**
58
     * Purchase Invoice.
59
     *
60
     * @return string
61
     */
62
    public function purchase()
63
    {
64
    	$mobile = $this->extract('mobile');
0 ignored issues
show
Bug introduced by
The method extract() does not exist on Shetabit\Payment\Drivers\Zarinpal. ( Ignorable by Annotation )

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

64
    	/** @scrutinizer ignore-call */ 
65
     $mobile = $this->extract('mobile');

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...
65
    	$description = $this->extract('description');
66
    	$factorNumber = $this->extract('factorNumber');
67
68
        $data = array(
69
            'api' => $this->settings->merchantId,
70
            'amount' => $this->invoice->getAmount(),
71
            'redirect' => $this->settings->callbackUrl,
72
            'mobile' => $mobile,
73
            'description' => $description,
74
            'factorNumber' => $factorNumber,
75
        );
76
77
        $response = $this->client->request(
78
            'POST',
79
            $this->settings->apiPurchaseUrl,
80
            ["json" => $data]
81
        );
82
        $body = json_decode($response->getBody()->getContents(), true);
83
84
        if (empty($body['Authority'])) {
85
            $body['Authority'] = null;
86
        } else {
87
            $this->invoice->transactionId($body['Authority']);
88
        }
89
90
        // return the transaction's id
91
        return $this->invoice->getTransactionId();
92
    }
93
94
    /**
95
     * Pay the Invoice
96
     *
97
     * @return \Illuminate\Http\RedirectResponse|mixed
98
     */
99
    public function pay()
100
    {
101
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
102
103
        // redirect using laravel logic
104
        return redirect()->to($payUrl);
105
    }
106
107
    /**
108
     * Verify payment
109
     *
110
     * @return mixed|void
111
     * @throws InvalidPaymentException
112
     * @throws \GuzzleHttp\Exception\GuzzleException
113
     */
114
    public function verify()
115
    {
116
        $data = [
117
            'api' => $this->settings->api,
118
            'token'  => $this->invoice->getTransactionId(),
119
        ];
120
121
        $response = $this->client->request(
122
            'POST',
123
            $this->settings->apiVerificationUrl,
124
            ['json' => $data]
125
        );
126
        $body = json_decode($response->getBody()->getContents(), true);
127
128
        if (!isset($body['Status']) || $body['Status'] != 100) {
129
            $this->notVerified($body['Status']);
130
        }
131
    }
132
133
    /**
134
     * Trigger an exception
135
     *
136
     * @param $status
137
     * @throws InvalidPaymentException
138
     */
139
    private function notVerified($status)
140
    {
141
        $translations = array(
142
            "-1" => "اطلاعات ارسال شده ناقص است.",
143
            "-2" => "IP و يا مرچنت كد پذيرنده صحيح نيست",
144
            "-3" => "با توجه به محدوديت هاي شاپرك امكان پرداخت با رقم درخواست شده ميسر نمي باشد",
145
            "-4" => "سطح تاييد پذيرنده پايين تر از سطح نقره اي است.",
146
            "-11" => "درخواست مورد نظر يافت نشد.",
147
            "-12" => "امكان ويرايش درخواست ميسر نمي باشد.",
148
            "-21" => "هيچ نوع عمليات مالي براي اين تراكنش يافت نشد",
149
            "-22" => "تراكنش نا موفق ميباشد",
150
            "-33" => "رقم تراكنش با رقم پرداخت شده مطابقت ندارد",
151
            "-34" => "سقف تقسيم تراكنش از لحاظ تعداد يا رقم عبور نموده است",
152
            "-40" => "اجازه دسترسي به متد مربوطه وجود ندارد.",
153
            "-41" => "اطلاعات ارسال شده مربوط به AdditionalData غيرمعتبر ميباشد.",
154
            "-42" => "مدت زمان معتبر طول عمر شناسه پرداخت بايد بين 30 دقيه تا 45 روز مي باشد.",
155
            "-54" => "درخواست مورد نظر آرشيو شده است",
156
            "101" => "عمليات پرداخت موفق بوده و قبلا PaymentVerification تراكنش انجام شده است.",
157
        );
158
        if (array_key_exists($status, $translations)) {
159
            throw new InvalidPaymentException($translations[$status]);
160
        } else {
161
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
162
        }
163
    }
164
}
165