This repository has been archived by the owner on Jun 29, 2018. It is now read-only.
forked from siddarth/Stripe.com-Wordpress-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-payment.php
91 lines (78 loc) · 2.32 KB
/
ajax-payment.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
<?php
// register the ajax process function with wordpress
add_action("wp_ajax_stripe_plugin_process_card", "stripe_plugin_process_card");
add_action("wp_ajax_nopriv_stripe_plugin_process_card", "stripe_plugin_process_card");
function stripe_plugin_process_card() {
// Use the globals defined in stripe.php
global $secretKey;
global $currencySymbol;
global $transPrefix;
// Create the response array
$response = array(
'success' => false
);
if($_POST) {
// Load the official Stripe PHP bootstrap file
require_once STRIPE_PAYMENTS_PLUGIN_DIR.'/stripe-php-1.5.19/lib/Stripe.php';
// Extract the extra data
$meta = array();
if($transPrefix) {
$meta['prefix'] = $transPrefix;
}
if($_POST['email']) {
$meta['email'] = $_POST['email'];
}
if($_POST['paymentId']) {
$meta['paymentId'] = $_POST['paymentId'];
}
// Create the data to submit to Stripe's secure processing
// Note: Card number data is not accessible. The code can
// only access a 'token' that was previously generated by
// Stripe via AJAX post.
$params = array(
'amount' => $_POST['amount'],
'currency' => $currencySymbol,
'card' => $_POST['token'],
'description' => array_implode(':=', '|', $meta)
);
// Submit the payment and charge the card.
try {
Stripe::setApiKey($secretKey);
$charge = Stripe_Charge::create($params);
// Charge was successful. Fill in response details.
$response['success'] = true;
$response['id'] = $charge->id;
$response['amount'] = number_format($charge->amount/100, 2);
$response['fee'] = number_format($charge->fee/100, 2);
$response['card_type'] = $charge->card->type;
$response['card_last4'] = $charge->card->last4;
$response['meta'] = $meta;
$response['desc'] = $params['description'];
} catch (Exception $e) {
$response['error'] = $e->getMessage();
}
}
// Add additional processing here
if($response['success']) {
// Succeess
} else {
// Failed
}
// Serialize the response back as JSON
echo json_encode($response);
die();
}
function array_implode ($glue, $separator, $array) {
if( ! is_array($array) ) {
return $array;
}
$string = array();
foreach( $array as $key => $val ) {
if( is_array( $val )) {
$val = implode(",", $val);
}
$string[] = "{$key}{$glue}{$val}";
}
return implode($separator, $string);
}
?>