-
Notifications
You must be signed in to change notification settings - Fork 3
/
stripe_pay_checkout_demo.php
114 lines (100 loc) · 3.29 KB
/
stripe_pay_checkout_demo.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
<?php
/**
* Stripe - Payment Gateway integration example (Stripe Checkout)
* ==============================================================================
*
* @version v1.0: stripe_pay_checkout_demo.php 2016/10/05
* @copyright Copyright (c) 2016, http://www.ilovephp.net
* @author Sagar Deshmukh <sagarsdeshmukh91@gmail.com>
* You are free to use, distribute, and modify this software
* ==============================================================================
*
*/
// Stripe library
require 'stripe/Stripe.php';
$params = array(
"testmode" => "on",
"private_live_key" => "sk_live_xxxxxxxxxxxxxxxxxxxxx",
"public_live_key" => "pk_live_xxxxxxxxxxxxxxxxxxxxx",
"private_test_key" => "sk_test_xxxxxxxxxxxxxxxxxxxxx",
"public_test_key" => "pk_test_xxxxxxxxxxxxxxxxxxxxx"
);
if ($params['testmode'] == "on") {
Stripe::setApiKey($params['private_test_key']);
$pubkey = $params['public_test_key'];
} else {
Stripe::setApiKey($params['private_live_key']);
$pubkey = $params['public_live_key'];
}
if(isset($_POST['stripeToken']))
{
$amount_cents = str_replace(".","","10.52"); // Chargeble amount
$invoiceid = "14526321"; // Invoice ID
$description = "Invoice #" . $invoiceid . " - " . $invoiceid;
try {
$charge = Stripe_Charge::create(array(
"amount" => $amount_cents,
"currency" => "usd",
"source" => $_POST['stripeToken'],
"description" => $description)
);
if ($charge->card->address_zip_check == "fail") {
throw new Exception("zip_check_invalid");
} else if ($charge->card->address_line1_check == "fail") {
throw new Exception("address_check_invalid");
} else if ($charge->card->cvc_check == "fail") {
throw new Exception("cvc_check_invalid");
}
// Payment has succeeded, no exceptions were thrown or otherwise caught
$result = "success";
} catch(Stripe_CardError $e) {
$error = $e->getMessage();
$result = "declined";
} catch (Stripe_InvalidRequestError $e) {
$result = "declined";
} catch (Stripe_AuthenticationError $e) {
$result = "declined";
} catch (Stripe_ApiConnectionError $e) {
$result = "declined";
} catch (Stripe_Error $e) {
$result = "declined";
} catch (Exception $e) {
if ($e->getMessage() == "zip_check_invalid") {
$result = "declined";
} else if ($e->getMessage() == "address_check_invalid") {
$result = "declined";
} else if ($e->getMessage() == "cvc_check_invalid") {
$result = "declined";
} else {
$result = "declined";
}
}
echo "<BR>Stripe Payment Status : ".$result;
echo "<BR>Stripe Response : ";
print_r($charge); exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Stripe Pay Checkout Demo</title>
</head>
<body>
<h1 class="bt_title" align="center">Stripe Pay Checkout Demo</h1>
<div align="center">
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $params['public_test_key']; ?>"
data-amount="999"
data-name="ILovePHP.net"
data-description="Donation"
data-image="http://www.ilovephp.net/wp-content/uploads/2016/03/small-3.jpg"
data-locale="auto"
data-zip-code="true">
</script>
</form>
</div>
</body>
</html>