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
|
|
|
use DB; |
10
|
|
|
|
11
|
|
|
class Mojo |
12
|
|
|
{ |
13
|
|
|
/* |
14
|
|
|
* Accepts the order details and creates a |
15
|
|
|
* payment request at Instamojo's end |
16
|
|
|
* returning the payment form URL |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
public static function giveMeFormUrl(User $user, $amount, $purpose, $phone = null) |
20
|
|
|
{ |
21
|
|
|
self::checkConfigValues(); |
22
|
|
|
|
23
|
|
|
$sub = config('laravelmojo.subdomain_for_endpoints'); |
24
|
|
|
|
25
|
|
|
$curl = self::setupCURL("https://{$sub}.instamojo.com/api/1.1/payment-requests/"); |
26
|
|
|
|
27
|
|
|
$payload = self::createPaymentPayload($user, $amount, $purpose, $phone); |
28
|
|
|
|
29
|
|
|
$response = self::closeCurl($curl,$payload); |
30
|
|
|
|
31
|
|
|
$finalResponse = json_decode($response); |
32
|
|
|
|
33
|
|
|
return $finalResponse->payment_request->longurl; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* After the payment via Instamojo, it |
38
|
|
|
* returns that payment's details |
39
|
|
|
* after redirection |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
public static function giveMePaymentDetails() |
43
|
|
|
{ |
44
|
|
|
$payment_id = filter_input(INPUT_GET, 'payment_id'); |
45
|
|
|
$payment_request_id = filter_input(INPUT_GET, 'payment_request_id'); |
46
|
|
|
$sub = config('laravelmojo.subdomain_for_endpoints'); |
47
|
|
|
|
48
|
|
|
$curl = self::setupCURL("https://{$sub}.instamojo.com/api/1.1/payment-requests/{$payment_request_id}/{$payment_id}/"); |
49
|
|
|
|
50
|
|
|
$response = curl_exec($curl); |
51
|
|
|
curl_close($curl); |
52
|
|
|
|
53
|
|
|
$decoded_response = json_decode($response); |
54
|
|
|
return $decoded_response->payment_request; |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* To process the refund's |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
public static function refund($payment_id,$type,$reason) |
63
|
|
|
{ |
64
|
|
|
$sub = config('laravelmojo.subdomain_for_endpoints'); |
65
|
|
|
|
66
|
|
|
$curl = static::setupCURL("https://{$sub}.instamojo.com/api/1.1/refunds/"); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$payload = ['payment_id' => $payment_id, |
69
|
|
|
'type' => $type, |
70
|
|
|
'body' => $reason ]; |
71
|
|
|
|
72
|
|
|
$response = self::closeCurl($curl,$payload); |
73
|
|
|
$afterDecoding = json_decode($response); |
74
|
|
|
$refund = $afterDecoding->refund; |
75
|
|
|
|
76
|
|
|
$transaction = MojoPaymentDetails::where('payment_id',$payment_id)->first(); |
77
|
|
|
$user_id = $transaction->user_id; |
78
|
|
|
|
79
|
|
|
$refund_record = self::createRefundInDB($user_id,$refund,$payment_id); |
80
|
|
|
|
81
|
|
|
return $refund_record; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private static function checkConfigValues() |
85
|
|
|
{ |
86
|
|
|
if (!config('laravelmojo.key')) { |
87
|
|
|
throw new Exception('Please set the Instamojo API key in your env file'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
elseif(!config('laravelmojo.token')) { |
91
|
|
|
throw new Exception('Please set the Instamojo token in your env file'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
elseif(!config('laravelmojo.redirect_url_after_payment')) { |
95
|
|
|
throw new Exception('Please set the redirect url in your env file'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
elseif(!config('laravelmojo.subdomain_for_endpoints')) { |
99
|
|
|
throw new Exception('Please set the subdomain for Instamojo api endpoint in your env file'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
elseif(!config('laravelmojo.webhook_url')) { |
103
|
|
|
throw new Exception('Please set the webhook url in your env file'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
elseif(!config('laravelmojo.salt')) { |
107
|
|
|
throw new Exception('Please set the instamojo salt in your env file'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
else { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private static function setupCURL($apiEndpoint) |
116
|
|
|
{ |
117
|
|
|
if (extension_loaded("curl")) { |
118
|
|
|
|
119
|
|
|
$ch = curl_init(); |
120
|
|
|
$api_key = config('laravelmojo.key'); |
121
|
|
|
$api_token = config('laravelmojo.token'); |
122
|
|
|
|
123
|
|
|
curl_setopt($ch, CURLOPT_URL, "$apiEndpoint"); |
124
|
|
|
curl_setopt($ch, CURLOPT_HEADER, FALSE); |
125
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
126
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); |
127
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER,["X-Api-Key:{$api_key}", |
128
|
|
|
"X-Auth-Token:{$api_token}"]); |
129
|
|
|
return $ch; |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
else { |
133
|
|
|
throw new Exception('CURL extension is not loaded'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private static function closeCurl($curl,$payload) |
138
|
|
|
{ |
139
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
140
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload)); |
141
|
|
|
$response = curl_exec($curl); |
142
|
|
|
curl_close($curl); |
143
|
|
|
|
144
|
|
|
return $response; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private static function createPaymentPayload(User $user, $amount, $purpose, $phone = null) |
148
|
|
|
{ |
149
|
|
|
if(is_null($phone)) { |
150
|
|
|
$phone = $user->phone; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$payload = ['purpose' => $purpose, |
154
|
|
|
'amount' => $amount, |
155
|
|
|
'phone' => $phone, |
156
|
|
|
'buyer_name' => $user->name, |
157
|
|
|
'redirect_url' => config('laravelmojo.redirect_url_after_payment'), |
158
|
|
|
'send_email' => false, |
159
|
|
|
'webhook' => config('laravelmojo.webhook_url'), |
160
|
|
|
'send_sms' => false, |
161
|
|
|
'email' => $user->email, |
162
|
|
|
'allow_repeated_payments' => false ]; |
163
|
|
|
|
164
|
|
|
return $payload; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private static function createRefundInDB($user_id,$refund,$payment_id) |
168
|
|
|
{ |
169
|
|
|
$refund_record = MojoRefundDetails::create(['user_id' => $user_id, |
170
|
|
|
'refund_id' => $refund->id, |
171
|
|
|
'payment_id' => $payment_id, |
172
|
|
|
'status' => $refund->status, |
173
|
|
|
'type' => $refund->type, |
174
|
|
|
'body' => $refund->body, |
175
|
|
|
'refund_amount' => $refund->refund_amount, |
176
|
|
|
'total_amount' => $refund->total_amount, |
177
|
|
|
]); |
178
|
|
|
|
179
|
|
|
return $refund_record; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public static function allPayments() |
183
|
|
|
{ |
184
|
|
|
return MojoPaymentDetails::all(); |
185
|
|
|
} |
186
|
|
|
public static function allPaymentsFor(User $user) |
187
|
|
|
{ |
188
|
|
|
return MojoPaymentDetails::where('user_id',$user->id)->get(); |
189
|
|
|
} |
190
|
|
|
public static function failedPayments() |
191
|
|
|
{ |
192
|
|
|
return MojoPaymentDetails::where('payment_status','!=','credit')->get(); |
193
|
|
|
} |
194
|
|
|
public static function successfulPayments() |
195
|
|
|
{ |
196
|
|
|
return MojoPaymentDetails::where('payment_status','credit')->get(); |
197
|
|
|
} |
198
|
|
|
public static function myAndMojosIncome() |
199
|
|
|
{ |
200
|
|
|
return MojoPaymentDetails::sum('amount'); |
201
|
|
|
} |
202
|
|
|
public static function myIncome() |
203
|
|
|
{ |
204
|
|
|
$a = MojoPaymentDetails::sum('amount'); |
205
|
|
|
$f = MojoPaymentDetails::sum('fees'); |
206
|
|
|
return $a - $f; |
207
|
|
|
} |
208
|
|
|
public static function mojosIncome() |
209
|
|
|
{ |
210
|
|
|
return MojoPaymentDetails::sum('fees'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public static function allRefunds() |
214
|
|
|
{ |
215
|
|
|
return MojoRefundDetails::all(); |
216
|
|
|
} |
217
|
|
|
public static function allRefundsFor(User $user) |
218
|
|
|
{ |
219
|
|
|
return MojoRefundDetails::where('user_id',$user->id)->get(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
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:In the case above, it makes sense to update
SomeClass
to useself
instead: