Passed
Pull Request — master (#176)
by
unknown
07:41
created

Payfa::notVerified()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Payfa;
4
5
6
use GuzzleHttp\Client;
7
use Shetabit\Multipay\Abstracts\Driver;
8
use Shetabit\Multipay\Contracts\ReceiptInterface;
9
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
10
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
11
use Shetabit\Multipay\Invoice;
12
use Shetabit\Multipay\Receipt;
13
use Shetabit\Multipay\RedirectionForm;
14
use Shetabit\Multipay\Request;
15
16
class Payfa extends Driver
17
{
18
    /**
19
     * Payfa Client.
20
     *
21
     * @var object
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
     * Payfa 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); // Set the invoice.
49
        $this->settings = (object) $settings; // Set settings.
50
        $this->client = new Client();
51
    }
52
53
    /**
54
     * Retrieve data from details using its name.
55
     *
56
     * @return string
57
     */
58
    private function extractDetails($name)
59
    {
60
        return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name];
61
    }
62
63
    public function purchase()
64
    {
65
        $mobile = $this->extractDetails('mobile');
66
        $cardNumber = $this->extractDetails('cardNumber');
67
68
        $data = array(
69
            'amount' => ($this->invoice->getAmount() * 10), // convert rial to toman
70
            'callbackUrl' => $this->settings->callbackUrl,
71
            'mobileNumber' => $mobile,
72
            'invoiceId' => $this->invoice->getUuid(),
73
            'cardNumber' => $cardNumber
74
        );
75
76
        $response = $this->client->request(
77
            'POST',
78
            $this->settings->apiPurchaseUrl,
79
            [
80
                "json" => $data,
81
                "http_errors" => false,
82
                "headers" => [
83
                    "X-API-Key" => $this->settings->apiKey,
84
                    'Content-Type' => 'application/json',
85
                ]
86
            ]
87
        );
88
        $body = json_decode($response->getBody()->getContents(), true);
89
90
91
        if ($response->getStatusCode() != 200) {
92
            throw new PurchaseFailedException($body["title"]);
93
        }
94
95
        $this->invoice->transactionId($body['paymentId']);
96
97
        // return the transaction's id
98
        return $this->invoice->getTransactionId();
99
    }
100
101
    /**
102
     * Pay the Invoice
103
     *
104
     * @return RedirectionForm
105
     */
106
    public function pay(): RedirectionForm
107
    {
108
        $payUrl = $this->settings->apiPaymentUrl . $this->invoice->getTransactionId();
109
110
        return $this->redirectWithForm($payUrl, [], 'GET');
111
    }
112
113
114
    /**
115
     * Verify payment
116
     *
117
     * @return ReceiptInterface
118
     *
119
     * @throws InvalidPaymentException
120
     * @throws \GuzzleHttp\Exception\GuzzleException
121
     */
122
    public function verify(): ReceiptInterface
123
    {
124
125
        $paymentId = $this->invoice->getTransactionId() ?? Request::input('paymentId');
126
127
128
        $response = $this->client->request(
129
            'POST',
130
            $this->settings->apiVerificationUrl . $paymentId,
131
            [
132
                "http_errors" => false,
133
                "headers" => [
134
                    "X-API-Key" => $this->settings->apiKey,
135
                    'Content-Type' => 'application/json',
136
                ]
137
            ]
138
        );
139
        $body = json_decode($response->getBody()->getContents(), true);
140
141
        if ($response->getStatusCode() != 200) {
142
            $this->notVerified($body["message"]);
143
        }
144
145
        return $this->createReceipt($body['transactionId']);
146
    }
147
148
    protected function createReceipt($referenceId)
149
    {
150
        return new Receipt('payfa', $referenceId);
151
    }
152
153
    /**
154
     * Trigger an exception
155
     *
156
     * @param $message
157
     * @throws InvalidPaymentException
158
     */
159
    private function notVerified($message)
160
    {
161
        if (empty($message)) {
162
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
163
        } else {
164
            throw new InvalidPaymentException($message);
165
        }
166
    }
167
}
168