-
Notifications
You must be signed in to change notification settings - Fork 3
/
payment.php
115 lines (85 loc) · 3.6 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
// Include configuration file
//require_once 'config.php';
define('STRIPE_API_KEY', 'sk_test_51HCO93CqIYx6oZ8JEgybBfrZbbjTrrxHCALbniAC6KWi9e13fyFbbHlvGqjFBYiJudjH4j2k6USiaIW4os1YCSJU00sxPbhkke');
define('STRIPE_PUBLISHABLE_KEY', 'pk_test_51HCO93CqIYx6oZ8JjQsqODDgKfsJrytUIlfBya0Gd2xDuP6j9rfxDPjTiJvAuPLSTzItfcXdMw0C4hKLBasEUSqq00qEfzsxTn');
$itemName = "Theme Development";
$product_amount = $_POST['itemprice'];
$itemPrice = $_GET['price'];
$currency = "USD";
$payment_id = $statusMsg = '';
$ordStatus = 'error';
// Check whether stripe token is not empty
if(!empty($_POST['stripeToken'])){
// Retrieve stripe token, card and user info from the submitted form data
$token = $_POST['stripeToken'];
$name = $_POST['name'];
$email = $_POST['email'];
// Include Stripe PHP library
require_once 'stripe-php/init.php';
// Set API key
\Stripe\Stripe::setApiKey(STRIPE_API_KEY);
// Add customer to stripe
try {
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token
));
}catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $customer){
// Convert price to cents
$itemPriceCents = ($itemPrice*100);
// Charge a credit or a debit card
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $itemPriceCents,
'currency' => $currency,
'description' => $itemName
));
}catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $charge){
// Retrieve charge details
$chargeJson = $charge->jsonSerialize();
// Check whether the charge is successful
if($chargeJson['amount_refunded'] == 0 && empty($chargeJson['failure_code']) && $chargeJson['paid'] == 1 && $chargeJson['captured'] == 1){
// Transaction details
$transactionID = $chargeJson['balance_transaction'];
$paidAmount = $chargeJson['amount'];
$paidAmount = ($paidAmount/100);
$paidCurrency = $chargeJson['currency'];
$payment_status = $chargeJson['status'];
// Include database connection file
echo "Done";
}
}else{
$statusMsg = "Charge creation failed! $api_error";
}
}else{
$statusMsg = "Invalid card details! $api_error";
}
}else{
$statusMsg = "Error on form submission.";
}
?>
<div class="container">
<div class="status">
<?php if(!empty($payment_id)){ ?>
<h4>Payment Information</h4>
<p><b>Reference Number:</b> <?php echo $payment_id; ?></p>
<p><b>Transaction ID:</b> <?php echo $transactionID; ?></p>
<p><b>Paid Amount:</b> <?php echo $paidAmount.' '.$paidCurrency; ?></p>
<p><b>Payment Status:</b> <?php echo $payment_status; ?></p>
<h4>Product Information</h4>
<p><b>Name:</b> <?php echo $itemName; ?></p>
<p><b>Price:</b> <?php echo $itemPrice.' '.$currency; ?></p>
<?php }else{ ?>
<h1 class="error">Your Payment has Failed</h1>
<?php } ?>
</div>
<a href="index.php" class="btn-link">Back to Payment Page</a>
</div>