Passed
Push — master ( 6fd703...fda00a )
by mahdi
03:25
created

Poolam::purchase()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 15
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 29
rs 9.7666
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 Poolam extends Driver
11
{
12
    /**
13
     * Poolam 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
     * Poolam 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
     * Purchase Invoice.
49
     *
50
     * @return string
51
     */
52
    public function purchase()
53
    {
54
        // convert to toman
55
        $toman = $this->invoice->getAmount() * 10;
56
57
        $data = array(
58
            'api_key' => $this->settings->merchantId,
59
            'amount' => $toman,
60
            'return_url' => $this->settings->callbackUrl,
61
        );
62
63
        $response = $this->client->request(
64
            'POST',
65
            $this->settings->apiPurchaseUrl,
66
            [
67
                "form_params" => $data,
68
                "http_errors" => false,
69
            ]
70
        );
71
        $body = json_decode($response->getBody()->getContents(), true);
72
73
        if (empty($body['status']) || $body['status'] != 1) {
74
            // error has happened
75
        } else {
76
            $this->invoice->transactionId($body['invoice_key']);
77
        }
78
79
        // return the transaction's id
80
        return $this->invoice->getTransactionId();
81
    }
82
83
    /**
84
     * Pay the Invoice
85
     *
86
     * @return \Illuminate\Http\RedirectResponse|mixed
87
     */
88
    public function pay()
89
    {
90
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
91
92
        // redirect using laravel logic
93
        return redirect()->to($payUrl);
94
    }
95
96
    /**
97
     * Verify payment
98
     *
99
     * @return mixed|void
100
     * @throws InvalidPaymentException
101
     * @throws \GuzzleHttp\Exception\GuzzleException
102
     */
103
    public function verify()
104
    {
105
        $data = [
106
            'api_key' => $this->settings->merchantId,
107
        ];
108
109
        $transactionId = $this->invoice->getTransactionId() ?? request()->input('invoice_key');
110
        $url = $this->settings->apiVerificationUrl.$transactionId;
111
112
        $response = $this->client->request(
113
            'POST',
114
            $url,
115
            ["form_params" => $data, "http_errors" => false]
116
        );
117
        $body = json_decode($response->getBody()->getContents(), true);
118
119
        if (empty($body['status']) || $body['status'] != 1) {
120
            $message = $body['errorDescription'] ?? null;
121
122
            $this->notVerified($message);
123
        }
124
    }
125
126
    /**
127
     * Trigger an exception
128
     *
129
     * @param $message
130
     * @throws InvalidPaymentException
131
     */
132
    private function notVerified($message)
133
    {
134
        if (empty($message)) {
135
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
136
        } else {
137
            throw new InvalidPaymentException($message);
138
        }
139
    }
140
}
141