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/"); |
|
|
|
|
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
|
|
|
|
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: