-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.php
99 lines (81 loc) · 3.8 KB
/
process.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
<?php
$paymentMessage = '';
if(!empty($_POST['stripeToken'])){
// get token and user details
$stripeToken = $_POST['stripeToken'];
$customerName = $_POST['customerName'];
$customerEmail = $_POST['emailAddress'];
$customerAddress = $_POST['customerAddress'];
$customerCity = $_POST['customerCity'];
$customerZipcode = $_POST['customerZipcode'];
$customerState = $_POST['customerState'];
$customerCountry = $_POST['customerCountry'];
$cardNumber = $_POST['cardNumber'];
$cardCVC = $_POST['cardCVC'];
$cardExpMonth = $_POST['cardExpMonth'];
$cardExpYear = $_POST['cardExpYear'];
print_r($_POST);
//include Stripe PHP library
require_once('stripe-php/init.php');
//set stripe secret key and publishable key
$stripe = array(
"secret_key" => "pk_test_B6cJ6r3MAZkwZdSvfHLVUffG",
"publishable_key" => "sk_test_mOrcHTINcIgblg2D70rirhDF"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
//add customer to stripe
$customer = \Stripe\Customer::create(array(
'name' => $customerName,
'description' => 'test description',
'email' => $customerEmail,
'source' => $stripeToken,
"address" => ["city" => $customerCity, "country" => $customerCountry, "line1" => $customerAddress, "line2" => "", "postal_code" => $customerZipcode, "state" => $customerState]
));
// item details for which payment made
$itemName = $_POST['item_details'];
$itemNumber = $_POST['item_number'];
$itemPrice = $_POST['price'];
$totalAmount = $_POST['total_amount'];
$currency = $_POST['currency_code'];
$orderNumber = $_POST['order_number'];
// details for which payment performed
$payDetails = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $totalAmount,
'currency' => $currency,
'description' => $itemName,
'metadata' => array(
'order_id' => $orderNumber
)
));
// get payment details
$paymenyResponse = $payDetails->jsonSerialize();
// check whether the payment is successful
if($paymenyResponse['amount_refunded'] == 0 && empty($paymenyResponse['failure_code']) && $paymenyResponse['paid'] == 1 && $paymenyResponse['captured'] == 1){
// transaction details
$amountPaid = $paymenyResponse['amount'];
$balanceTransaction = $paymenyResponse['balance_transaction'];
$paidCurrency = $paymenyResponse['currency'];
$paymentStatus = $paymenyResponse['status'];
$paymentDate = date("Y-m-d H:i:s");
//insert tansaction details into database
include_once("connect.php");
$insertTransactionSQL = "INSERT INTO transaction(cust_name, cust_email, card_number, card_cvc, card_exp_month, card_exp_year,item_name, item_number, item_price, item_price_currency, paid_amount, paid_amount_currency, txn_id, payment_status, created, modified)
VALUES('".$customerName."','".$customerEmail."','".$cardNumber."','".$cardCVC."','".$cardExpMonth."','".$cardExpYear."','".$itemName."','".$itemNumber."','".$itemPrice."','".$paidCurrency."','".$amountPaid."','".$paidCurrency."','".$balanceTransaction."','".$paymentStatus."','".$paymentDate."','".$paymentDate."')";
mysqli_query($conn, $insertTransactionSQL) or die("database error: ". mysqli_error($conn));
$lastInsertId = mysqli_insert_id($conn);
//if order inserted successfully
if($lastInsertId && $paymentStatus == 'succeeded'){
$paymentMessage = "The payment was successful. Order ID: {$orderNumber}";
} else{
$paymentMessage = "failed";
}
} else{
$paymentMessage = "failed";
}
} else{
$paymentMessage = "failed";
}
$_SESSION["message"] = $paymentMessage;
header('location:index.php');
?>