BsecurePayments   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 91
c 1
b 0
f 0
dl 0
loc 174
rs 10
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A orderStatusUpdates() 0 9 2
A setTransactionDetails() 0 43 5
A setCustomerAddress() 0 31 4
A createOrder() 0 16 3
A setCustomer() 0 30 4
1
<?php
2
3
namespace bSecure\Payments;
4
5
use bSecure\Payments\Controllers\Orders\CreateOrderController;
6
use bSecure\Payments\Controllers\Orders\IOPNController;
7
use bSecure\Payments\Controllers\Orders\OrderStatusUpdateController;
0 ignored issues
show
Bug introduced by
The type bSecure\Payments\Control...rStatusUpdateController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
use bSecure\Payments\Helpers\ApiResponseHandler;
10
use bSecure\Payments\Helpers\Constant;
11
use bSecure\Payments\Helpers\Helper;
12
use Illuminate\Support\Facades\Facade;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Facade was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Rakit\Validation\Validator;
14
15
class BsecurePayments extends Facade
16
{
17
18
    private $orderPayload = [
19
        'plugin_version' => null,
20
        'redirect_url' => null,
21
        'hash' => null,
22
        'merchant_id' => null,
23
        'store_id' => null,
24
        'customer' => null,
25
        'customer_address' => null,
26
        'order' => null,
27
    ];
28
29
    /*
30
     *  CREATE ORDER: Set Order Id
31
    */
32
    public function setTransactionDetails($details)
33
    {
34
        try {
35
            if(empty($details))
36
            {
37
                return ApiResponseHandler::validationError("Transaction details are required");
38
            }
39
40
            $validator = new Validator;
41
            $validation = $validator->make($details, [
42
                'order_id' => 'required',
43
                'transaction_dt' => 'required',
44
                'sub_total_amt' => 'required',
45
                'discount_amt' => 'required',
46
                'total_amt' => 'required',
47
                'redirect_url' => 'required',
48
            ]);
49
            // then validate
50
            $validation->validate();
51
            //Now check validation:
52
            if ($validation->fails())
53
            {
54
                return ApiResponseHandler::validationError($validation->errors());
55
            }
56
57
            $this->orderPayload['order'] = [
58
                "order_id" => $details['order_id'],
59
                "currency" => 'PKR',
60
                "sub_total_amount" => $details['sub_total_amt'],
61
                "discount_amount" => $details['discount_amt'],
62
                "total_amount" => $details['total_amt']
63
            ];
64
            $this->orderPayload['plugin_version'] = Constant::PACKAGE_VERISON;
65
            $this->orderPayload['env_id'] = config('bSecurePayments.integration_type') == "sandbox" ? 2 : 1;
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

65
            $this->orderPayload['env_id'] = /** @scrutinizer ignore-call */ config('bSecurePayments.integration_type') == "sandbox" ? 2 : 1;
Loading history...
66
            $this->orderPayload['txn_reference'] = $details['transaction_dt'];
67
            $this->orderPayload['redirect_url'] = $details['redirect_url'];
68
            $this->orderPayload['merchant_id'] = config('bSecurePayments.merchant_id');
69
            $this->orderPayload['store_id'] = config('bSecurePayments.store_slug');
70
71
            return $this->orderPayload;
72
            //code...
73
        } catch (\Throwable $th) {
74
            throw $th;
75
        }
76
    }
77
78
79
    /*
80
     *  CREATE ORDER: Set Customer Payload
81
    */
82
    public function setCustomer($customerData)
83
    {
84
        try {
85
            if(empty($customerData))
86
            {
87
                return ApiResponseHandler::validationError("Customer data is required");
88
            }
89
90
            $validator = new Validator;
91
            $validation = $validator->make($customerData, [
92
                'country_code' => 'required',
93
                'phone_number' => 'required',
94
                'name' => 'required',
95
                'email' => 'required',
96
            ]);
97
            // then validate
98
            $validation->validate();
99
            //Now check validation:
100
            if ($validation->fails())
101
            {
102
                return ApiResponseHandler::validationError($validation->errors());
103
            }
104
105
            $order = new CreateOrderController();
106
            $customer = $order->_setCustomer($customerData);
107
            $this->orderPayload['customer'] = $customer;
108
            return $this->orderPayload;
109
            //code...
110
        } catch (\Throwable $th) {
111
            throw $th;
112
        }
113
    }
114
115
116
    /*
117
     *  CREATE ORDER: Set Customer Address Payload
118
    */
119
    public function setCustomerAddress($customerData)
120
    {
121
        try {
122
            if(empty($customerData))
123
            {
124
                return ApiResponseHandler::validationError("Customer address data is required");
125
            }
126
127
            $validator = new Validator;
128
            $validation = $validator->make($customerData, [
129
                'country' => 'required',
130
                'province' => 'required',
131
                'city' => 'required',
132
                'area' => 'required',
133
                'address' => 'required',
134
            ]);
135
            // then validate
136
            $validation->validate();
137
            //Now check validation:
138
            if ($validation->fails())
139
            {
140
                return ApiResponseHandler::validationError($validation->errors());
141
            }
142
143
            $order = new CreateOrderController();
144
            $customer = $order->_setCustomerAddress($customerData);
145
            $this->orderPayload['customer_address'] = $customer;
146
            return $this->orderPayload;
147
            //code...
148
        } catch (\Throwable $th) {
149
            throw $th;
150
        }
151
    }
152
153
154
    /*
155
     *  CREATE ORDER: Create Order using Merchant Access Token from Merchant backend server
156
    */
157
    public function createOrder()
158
    {
159
        try {
160
            if(empty($this->orderPayload))
161
            {
162
                return ApiResponseHandler::validationError("Transaction data is required");
163
            }
164
165
            $order = new CreateOrderController();
166
            $this->orderPayload['hash'] = Helper::calculateSecureHash($this->orderPayload);
167
            $result = $order->create($this->orderPayload);
168
            return json_decode($result->getContent(), true);
169
//            return $result->getContent();
170
            //code...
171
        } catch (\Throwable $th) {
172
            throw $th;
173
        }
174
    }
175
176
    /*
177
     *  INSTANT ORDER PROCESSING NOTIFICATIONS : Get order status for merchant
178
    */
179
180
    public function orderStatusUpdates($order_ref = null)
181
    {
182
        try {
183
            $customer = new IOPNController();
184
            $result = $customer->orderStatus($order_ref);
185
            return json_decode($result->getContent(), true);
186
            //code...
187
        } catch (\Throwable $th) {
188
            throw $th;
189
        }
190
    }
191
192
}
193