Passed
Push — master ( 437f23...0ad4c0 )
by mahdi
03:18
created

Yekpay::purchase()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 43
rs 9.488
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Shetabit\Payment\Drivers\Yekpay;
4
5
use Shetabit\Payment\Abstracts\Driver;
6
use Shetabit\Payment\Exceptions\{InvalidPaymentException, PurchaseFailedException};
7
use Shetabit\Payment\{Contracts\ReceiptInterface, Invoice, Receipt};
8
9
class Yekpay 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
     * Yekpay 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
     * Retrieve data from details using its name.
40
     *
41
     * @return string
42
     */
43
    private function extractDetails($name)
44
    {
45
        return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name];
46
    }
47
48
    /**
49
     * Purchase Invoice.
50
     *
51
     * @return string
52
     *
53
     * @throws PurchaseFailedException
54
     * @throws \SoapFault
55
     */
56
    public function purchase()
57
    {
58
        $client = new \SoapClient($this->settings->apiPurchaseUrl, array('trace' => true));
59
60
        $data = new \stdClass();
61
62
        if (!empty($this->invoice->getDetails()['description'])) {
63
            $description = $this->invoice->getDetails()['description'];
64
        } else {
65
            $description = $this->settings->description;
66
        }
67
68
        $data->merchantId = $this->settings->merchantId;
69
        $data->amount = $this->invoice->getAmount();
70
        $data->callback = $this->settings->callbackUrl;
71
        $data->orderNumber = intval(1, time()) . crc32($this->invoice->getUuid());
72
73
        $data->fromCurrencyCode = 978;
74
        $data->toCurrencyCode = 364;
75
76
        $data->firstName = $this->extractDetails('firstName');
77
        $data->lastName = $this->extractDetails('lastName');
78
        $data->email = $this->extractDetails('email');
79
        $data->mobile = $this->extractDetails('mobile');
80
81
        $data->address = $this->extractDetails('address');
82
        $data->country = $this->extractDetails('country');
83
        $data->postalCode = $this->extractDetails('postalCode');
84
        $data->city = $this->extractDetails('city');
85
86
        $data->description = $description;
87
88
        $response = json_decode($client->request($data));
89
90
        if ($response->Code == 100) {
91
            $this->invoice->transactionId($response->Authority);
92
        } else {
93
            //"Request failed with Error code: $response->Code and Error message: $response->Description";
94
            throw new PurchaseFailedException($response->Description);
95
        }
96
97
        // return the transaction's id
98
        return $this->invoice->getTransactionId();
99
    }
100
101
    /**
102
     * Pay the Invoice
103
     *
104
     * @return \Illuminate\Http\RedirectResponse|mixed
105
     */
106
    public function pay()
107
    {
108
        $payUrl = $this->settings->apiPaymentUrl . $this->invoice->getTransactionId();
109
110
        // redirect using laravel logic
111
        return redirect()->to($payUrl);
112
    }
113
114
    /**
115
     * Verify payment
116
     *
117
     * @return mixed|void
118
     *
119
     * @throws InvalidPaymentException
120
     * @throws \GuzzleHttp\Exception\GuzzleException
121
     */
122
    public function verify() : ReceiptInterface
123
    {
124
        $options = array('trace' => true);
125
        $client = new SoapClient($this->settings->apiVerificationUrl, $options);
0 ignored issues
show
Bug introduced by
The type Shetabit\Payment\Drivers\Yekpay\SoapClient was not found. Did you mean SoapClient? If so, make sure to prefix the type with \.
Loading history...
126
127
        $data = new \stdClass();
128
129
        $data->merchantId = $this->settings->merchantId;
130
        $data->authority = $this->invoice->getTransactionId() ?? request()->input('authority');
131
132
        $response = json_decode($client->verify($data));
133
134
        if ($response->Code != 100) {
135
            $this->notVerified($transaction->message ?? 'payment failed');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $transaction seems to be never defined.
Loading history...
136
        }
137
138
        //"Success Payment with reference: $response->Reference and message: $transaction->message";
139
        return $this->createReceipt($response->Reference);
140
    }
141
142
    /**
143
     * Generate the payment's receipt
144
     *
145
     * @param $referenceId
146
     *
147
     * @return Receipt
148
     */
149
    public function createReceipt($referenceId)
150
    {
151
        $receipt = new Receipt('yekpay', $referenceId);
152
153
        return $receipt;
154
    }
155
156
    /**
157
     * Trigger an exception
158
     *
159
     * @param $message
160
     * @throws InvalidPaymentException
161
     */
162
    private function notVerified($message)
163
    {
164
        if ($message) {
165
            throw new InvalidPaymentException($message);
166
        } else {
167
            throw new InvalidPaymentException('payment failed');
168
        }
169
    }
170
}
171