Mojo   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 195
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A giveMeFormUrl() 0 16 1
A giveMePaymentDetails() 0 14 1
A refund() 0 21 1
B checkConfigValues() 0 18 7
A setupCURL() 0 18 2
A closeCurl() 0 9 1
A createPaymentPayload() 0 19 2
A createRefundInDB() 0 14 1
A allPayments() 0 4 1
A allPaymentsFor() 0 4 1
A failedPayments() 0 4 1
A successfulPayments() 0 4 1
A myAndMojosIncome() 0 4 1
A myIncome() 0 6 1
A mojosIncome() 0 4 1
A allRefunds() 0 4 1
A allRefundsFor() 0 4 1
1
<?php
2
3
namespace Lubusin\Mojo;
4
5
use Lubusin\Mojo\Models\MojoPaymentDetails;
6
use Lubusin\Mojo\Models\MojoRefundDetails;
7
use Exception;
8
use App\User;
9
10
class Mojo
11
{
12
    /*
13
     * Accepts the order details and creates a
14
     * payment request at Instamojo's end
15
     * returning the payment form URL
16
     */
17
18
    public static function giveMeFormUrl(User $user, $amount, $purpose, $phone = null)
19
    {
20
        self::checkConfigValues();
21
22
        $sub = config('laravelmojo.subdomain_for_endpoints');
23
24
        $curl = self::setupCURL("https://{$sub}.instamojo.com/api/1.1/payment-requests/");
25
26
        $payload = self::createPaymentPayload($user, $amount, $purpose, $phone);
27
28
        $response = self::closeCurl($curl, $payload);
29
30
        $finalResponse = json_decode($response);
31
        
32
        return $finalResponse->payment_request->longurl;
33
    }
34
35
    /*
36
     * After the payment via Instamojo, it
37
     * returns that payment's details
38
     * after redirection
39
     */
40
41
    public static function giveMePaymentDetails()
42
    {
43
        $payment_id = filter_input(INPUT_GET, 'payment_id');
44
        $payment_request_id = filter_input(INPUT_GET, 'payment_request_id');
45
        $sub = config('laravelmojo.subdomain_for_endpoints');
46
47
        $curl = self::setupCURL("https://{$sub}.instamojo.com/api/1.1/payment-requests/{$payment_request_id}/{$payment_id}/");
48
49
        $response = curl_exec($curl);
50
        curl_close($curl);
51
52
        $decoded_response = json_decode($response);
53
        return $decoded_response->payment_request;
54
    }
55
56
    /*
57
     * To process the refund's
58
     */
59
60
    public static function refund($payment_id, $type, $reason)
61
    {
62
        $sub = config('laravelmojo.subdomain_for_endpoints');
63
64
        $curl = static::setupCURL("https://{$sub}.instamojo.com/api/1.1/refunds/");
0 ignored issues
show
Bug introduced by
Since setupCURL() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of setupCURL() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
65
66
        $payload = ['payment_id' => $payment_id,
67
                    'type' => $type,
68
                    'body' => $reason ];
69
70
        $response = self::closeCurl($curl, $payload);
71
        $afterDecoding = json_decode($response);
72
        $refund = $afterDecoding->refund;
73
74
        $transaction = MojoPaymentDetails::where('payment_id', $payment_id)->first();
75
        $user_id = $transaction->user_id;
76
77
        $refund_record = self::createRefundInDB($user_id, $refund, $payment_id);
78
            
79
        return $refund_record;
80
    }
81
82
    private static function checkConfigValues()
83
    {
84
        if (!config('laravelmojo.key')) {
85
            throw new Exception('Please set the Instamojo API key in your env file');
86
        } elseif (!config('laravelmojo.token')) {
87
            throw new Exception('Please set the Instamojo token in your env file');
88
        } elseif (!config('laravelmojo.redirect_url_after_payment')) {
89
            throw new Exception('Please set the redirect url in your env file');
90
        } elseif (!config('laravelmojo.subdomain_for_endpoints')) {
91
            throw new Exception('Please set the subdomain for Instamojo api endpoint in your env file');
92
        } elseif (!config('laravelmojo.webhook_url')) {
93
            throw new Exception('Please set the webhook url in your env file');
94
        } elseif (!config('laravelmojo.salt')) {
95
            throw new Exception('Please set the instamojo salt in your env file');
96
        } else {
97
            return true;
98
        }
99
    }
100
101
    private static function setupCURL($apiEndpoint)
102
    {
103
        if (extension_loaded("curl")) {
104
            $ch = curl_init();
105
            $api_key = config('laravelmojo.key');
106
            $api_token = config('laravelmojo.token');
107
108
            curl_setopt($ch, CURLOPT_URL, "$apiEndpoint");
109
            curl_setopt($ch, CURLOPT_HEADER, false);
110
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
111
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
112
            curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Api-Key:{$api_key}",
113
                                                 "X-Auth-Token:{$api_token}"]);
114
            return $ch;
115
        } else {
116
            throw new Exception('CURL extension is not loaded');
117
        }
118
    }
119
120
    private static function closeCurl($curl, $payload)
121
    {
122
        curl_setopt($curl, CURLOPT_POST, true);
123
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload));
124
        $response = curl_exec($curl);
125
        curl_close($curl);
126
127
        return $response;
128
    }
129
130
    private static function createPaymentPayload(User $user, $amount, $purpose, $phone = null)
131
    {
132
        if (is_null($phone)) {
133
            $phone = $user->phone;
134
        }
135
136
        $payload = ['purpose' => $purpose,
137
                    'amount' => $amount,
138
                    'phone' => $phone,
139
                    'buyer_name' => $user->name,
140
                    'redirect_url' => config('laravelmojo.redirect_url_after_payment'),
141
                    'send_email' => false,
142
                    'webhook' => config('laravelmojo.webhook_url'),
143
                    'send_sms' => false,
144
                    'email' => $user->email,
145
                    'allow_repeated_payments' => false ];
146
147
        return $payload;
148
    }
149
150
    private static function createRefundInDB($user_id, \stdClass $refund, $payment_id)
151
    {
152
        $refund_record = MojoRefundDetails::create(['user_id' => $user_id,
153
                                                   'refund_id' => $refund->id,
154
                                                   'payment_id' => $payment_id,
155
                                                   'status' => $refund->status,
156
                                                   'type' => $refund->type,
157
                                                   'body' => $refund->body,
158
                                                   'refund_amount' => $refund->refund_amount,
159
                                                   'total_amount' => $refund->total_amount,
160
                                                 ]);
161
162
        return $refund_record;
163
    }
164
165
    public static function allPayments()
166
    {
167
        return MojoPaymentDetails::all();
168
    }
169
    public static function allPaymentsFor(User $user)
170
    {
171
        return MojoPaymentDetails::where('user_id', $user->id)->get();
172
    }
173
    public static function failedPayments()
174
    {
175
        return MojoPaymentDetails::where('payment_status', '!=', 'credit')->get();
176
    }
177
    public static function successfulPayments()
178
    {
179
        return MojoPaymentDetails::where('payment_status', 'credit')->get();
180
    }
181
    public static function myAndMojosIncome()
182
    {
183
        return MojoPaymentDetails::sum('amount');
184
    }
185
    public static function myIncome()
186
    {
187
        $a = MojoPaymentDetails::sum('amount');
188
        $f = MojoPaymentDetails::sum('fees');
189
        return $a - $f;
190
    }
191
    public static function mojosIncome()
192
    {
193
        return MojoPaymentDetails::sum('fees');
194
    }
195
196
    public static function allRefunds()
197
    {
198
        return MojoRefundDetails::all();
199
    }
200
    public static function allRefundsFor(User $user)
201
    {
202
        return MojoRefundDetails::where('user_id', $user->id)->get();
203
    }
204
}
205