Passed
Pull Request — master (#109)
by mohammad
19:24
created

Digipay::verify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
4
namespace Shetabit\Multipay\Drivers\Digipay;
5
6
use Shetabit\Multipay\Abstracts\Driver;
7
use Shetabit\Multipay\Contracts\ReceiptInterface;
8
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
9
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
10
use Shetabit\Multipay\Invoice;
11
use Shetabit\Multipay\Receipt;
12
use Shetabit\Multipay\RedirectionForm;
13
use Shetabit\Multipay\Request;
14
15
class Digipay extends Driver
16
{
17
    /**
18
     * Invoice
19
     *
20
     * @var Invoice
21
     */
22
    protected $invoice;
23
24
    /**
25
     * Driver settings
26
     *
27
     * @var object
28
     */
29
    protected $settings;
30
    /**
31
     * Digipay AccessToken
32
     *
33
     * @var string
34
     */
35
    protected $accessToken;
36
37
    /**
38
     * Digipay constructor.
39
     * Construct the class with the relevant settings.
40
     *
41
     * @param Invoice $invoice
42
     * @param $settings
43
     */
44
    public function __construct(Invoice $invoice, $settings)
45
    {
46
        $this->invoice($invoice);
47
        $this->setUpHttpClient($settings->apiBaseUrl);
48
        $this->settings=$settings;
49
        $this->accessToken = $this->getAccessToken();
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function purchase(): string
56
    {
57
        $details = $this->invoice->getDetails();
58
        $phone = $details['phone']??$details['mobile']??null;
59
        $data = [
60
            'amount' => $this->invoice->getAmount(),
61
            'phone' => $phone,
62
            'providerId' => $this->invoice->getUuid(),
63
            'redirectUrl' => $this->settings->callbackUrl,
64
            'type' => 0,
65
            'userType' => is_null($phone) ? 2 : 0
66
        ];
67
        $response = $this->client->post($this->settings->apiPurchaseUrl, $data, [
68
            'Authorization' => 'Bearer '.$this->accessToken
69
        ]);
70
        $response->throwError(PurchaseFailedException::class, 'عملیات پرداخت با خطا مواجه شد');
71
        $this->invoice->transactionId($response['ticket']);
72
        return $this->invoice->getTransactionId();
73
    }
74
75
    /**
76
     * @return RedirectionForm
77
     */
78
    public function pay(): RedirectionForm
79
    {
80
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
81
82
        return $this->redirectWithForm($payUrl, [], 'GET');
83
    }
84
85
    /**
86
     * @return ReceiptInterface
87
     */
88
    public function verify(): ReceiptInterface
89
    {
90
        $tracingId=Request::input("trackingCode");
91
        $header =[
92
            "Accept" => "application/json",
93
            "Authorization" => "Bearer ".$this->accessToken
94
        ];
95
        $response = $this->client->request('POST', $this->settings->apiVerificationUrl.$tracingId, [], $header);
96
97
98
        $response->throwError(InvalidPaymentException::class, 'پرداخت تایید نشد');
99
100
        return new Receipt('digipay', $response["trackingCode"]);
101
    }
102
103
104
    /**
105
     * @return string
106
     */
107
    protected function getAccessToken() : string
108
    {
109
        $data = [
110
            "username" => $this->settings->username,
111
            "password" => $this->settings->password,
112
            "grant_type" => 'password',
113
        ];
114
        $header = [
115
            'Content-Type' => 'multipart/form-data',
116
            'Authorization' => 'Basic '.base64_encode("{$this->settings->client_id}:{$this->settings->client_secret}")
117
        ];
118
        $response = $this->client->request('POST', $this->settings->apiOauthUrl, $data, $header);
119
        $response->setStatusMessages(401, 'خطا نام کاربری یا رمز عبور شما اشتباه می باشد.');
120
        $response->throwError(PurchaseFailedException::class);
121
        return $response['access_token'];
122
    }
123
}
124