forked from rdpse/GateCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripecx.php
269 lines (207 loc) · 8.15 KB
/
stripecx.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/*
**********************************************
*** GateCX Checkout for WHMCS ***
File: stripecx.php
File version: 0.0.3
Date: 27-10-2015
Copyright (C) NetDistrict 2014 - 2016
All Rights Reserved
**********************************************
*/
use Illuminate\Database\Capsule\Manager as Capsule;
function stripecx_config() {
$pdo = Capsule::connection()->getPdo();
$result = $pdo->query("SHOW TABLES LIKE 'stripecx_transactions'");
$row = $result->fetch(PDO::FETCH_ASSOC);
if($row!=false) $db_table = 'Uninstall';
else $db_table = 'Install';
$configarray = array(
"FriendlyName" => array("Type" => "System", "Value"=>"GateCX"),
"private-key" => array("FriendlyName" => "Private Key", "Type" => "text", "Size" => "20", ),
"public-key" => array("FriendlyName" => "Public Key", "Type" => "text", "Size" => "20", ),
"validatezip" => array("FriendlyName" => "Validate ZIP Code", "Type" => "yesno", "Description" => "Specify whether Checkout should validate the billing ZIP code.", ),
"rememberme" => array("FriendlyName" => "Remember Me", "Type" => "yesno", "Description" => "Specify whether to include the option to \"Remember Me\" for future purchases (true or false).", ),
"sendfailedmail" => array("FriendlyName" => "Send Failed Mail", "Type" => "yesno", "Description" => "Send an e-mail to the client when the credit card payment fails (recommended).", ),
"image-url" => array("FriendlyName" => "Image URL", "Type" => "text", "Size" => "100%", "Description" => "<br />A URL pointing to a square image of your brand or product. The recommended minimum size is 128x128px",),
"setup" => array("FriendlyName" => "Transaction table", "Description" => "<input class=\"btn btn-default btn-sm\" type=button onClick=\"location.href='?stripecx=".$db_table."'\" value='".$db_table."'> Tables must be installed in order to use this gateway.<br /><small style=\"float:right;\">Copyright © 2014 - 2016 <a href=\"http://www.netdistrict.co.uk\">NetDistrict</a>. All rights reserved.</small>",),
);
return $configarray;
}
function stripecx_link($params) {
global $whmcs;
$pdo = Capsule::connection()->getPdo();
# Invoice Variables
$invoiceid = $params['invoiceid'];
$description = $params["description"];
$amount = $params['amount']; # Format: ##.##
$currency = $params['currency']; # Currency Code
# Client Variables
$email = $params['clientdetails']['email'];
$postcode = $params['clientdetails']['postcode'];
# System Variables
$companyname = $params['companyname'];
$systemurl = $params['systemurl'];
# Config Options
if ($params['validatezip'] == 'on') { $zipCodeValidation = 'true'; } else { $zipCodeValidation = 'false'; }
if ($params['rememberme'] == 'on') { $RememberMe = 'true'; } else { $RememberMe = 'false'; }
# Redirect If Checkout From Cart
$cart = $_REQUEST['a'];
if ($cart == 'complete') {
header('Location: viewinvoice.php?id='.$invoiceid.'&stripecx');
}
# Auto-Start StripeCX?
if (isset($_REQUEST['stripecx'])) {
$start_sx = true;
} else {
$start_sx = false;
}
$sql = "SELECT * FROM stripecx_transactions WHERE invoice_id = '$invoiceid'";
$result = $pdo->query($sql);
$row = $result->fetch(PDO::FETCH_ASSOC);
if ($row){
$pending_trans = "
<script>
$(document).ready(function() {
$('select[name=\"gateway\"]').attr('disabled', 'disabled');
$('#StripeCXbutton').attr('disabled', 'disabled');
$('#StripeCXbutton').text('".$whmcs->get_lang('clientareapending')."');
});
function submit() {
}
</script>
";
} else { $pending_trans = NULL; }
# Enter your code submit to the gateway...
$code = "
<script src=\"https://checkout.stripe.com/v2/checkout.js\"></script>
<script src=\"assets/js/jquery.js\"></script>
".$pending_trans."
<form>
<button id=\"StripeCXbutton\">".$params['langpaynow']."</button>
</form>
<script>
$ = jQuery;
var handler = StripeCheckout.configure({
key: '".$params['public-key']."',
image: '".$params['image-url']."',
currency: '".$currency."',
email: '".$email."',
zipCode: '".$zipCodeValidation."',
allowRememberMe: '".$RememberMe."',
description: '".str_replace($companyname." - ","",$description)."',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
$('select[name=\"gateway\"]').attr('disabled', 'disabled');
$('#StripeCXbutton').attr('disabled', 'disabled');
$('#StripeCXbutton').text('".$whmcs->get_lang('clientareapending')."');
storeTransaction('".$invoiceid."',token.id, '".$currency."', '".$amount*100 ."');
}
});
document.getElementById('StripeCXbutton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: '".$companyname."',
description: '".str_replace($companyname." - ","",$description)."',
amount: ".$amount*100 ."
});
e.preventDefault();
});
function storeTransaction(sid,stoken,currency, amount) {
url = document.URL;
url = url.substring(0, url.lastIndexOf('/') + 1);
$.ajax({
data: { id: sid, token: stoken, currency: currency, amount: amount },
url: url+'modules/gateways/stripecx/callback.php',
method: \"POST\",
success: function(data) {
}
});
}
</script>
";
if ($start_sx == true) {
$code = $code ."
<script>
document.getElementById('StripeCXbutton').click();
</script>
";
}
return $code;
}
function stripecx_refund($params) {
# Invoice Variables
$invoiceid = $params['invoiceid'];
$transid = $params['transid']; # Transaction ID of Original Payment
$amount = $params['amount']; # Format: ##.##
$currency = $params['currency']; # Currency Code
# Perform Refund Here & Generate $results Array, eg:
$refund= json_decode(stripecx_createrefund($params, $transid, $amount));
$refundid = $refund->refunds->data[0]->id;
$logdata = "Transaction ID: ".$refundid."\r\nInvoice ID: ".$invoiceid."\r\nStatus: charge.send";
# Return Results
if ($refundid != NULL) {
return array("status"=>"success","transid"=>$refundid,"rawdata"=>$logdata);
} else {
return array("status"=>"error","rawdata"=>$refund->error->message);
}
}
function stripecx_createrefund($params,$transid,$amount)
{
// Check if custom amount is set
if ($amount != NULL) $data = '&amount='.$amount*100;
else $data = NULL;
// Send refund request
$ch = curl_init('https://api.stripe.com/v1/charges/'.$transid.'/refund');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Client: GateCX v0.0.3'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $params['private-key'] . ":" . NULL);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__) . "/stripecx/cacert.pem"));
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
if ($_REQUEST['stripecx'] == 'Install') {
// Install required database table
$pdo = Capsule::connection()->getPdo();
$pdo->beginTransaction();
try {
$query = "CREATE TABLE IF NOT EXISTS `stripecx_transactions` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`invoice_id` int(32) NOT NULL,
`transaction_id` varchar(512) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
$statement = $pdo->prepare($query);
$statement->execute();
// COMMIT QUERYS
$pdo->commit();
} catch (\Exception $e) {
$pdo->rollBack();
logActivity('Error during GateCX activation: '.$e->getMessage());
}
}
if ($_REQUEST['stripecx'] == 'Uninstall') {
// Remove required database table
$pdo = Capsule::connection()->getPdo();
$pdo->beginTransaction();
try {
$query = "DROP TABLE IF EXISTS `stripecx_transactions`;";
$statement = $pdo->prepare($query);
$statement->execute();
// COMMIT QUERYS
$pdo->commit();
} catch (\Exception $e) {
$pdo->rollBack();
logActivity('Error during GateCX deactivation: '.$e->getMessage());
}
}
?>