· 5 years ago · Dec 13, 2019, 04:35 AM
1<?php
2//check whether stripe token is not empty
3if(isset($_POST["stripeSource"]) && !empty($_POST["stripeSource"])) {
4 //get token, card and user info from the form
5 $source = $_POST['stripeSource'];
6 $name = $_POST['name'];
7 $email = $_POST['email'];
8 $userid = $_POST['userid'];
9 $totalamount = $_POST['totalamount'];
10
11
12 //include Stripe PHP library
13 require_once('stripe-php/init.php');
14 //set api key
15 $stripe = array(
16 "secret_key" => "sk_test_9pOerxcSe3JipvIL2NRCBctF00X2NQT3oi",
17 "publishable_key" => "pk_test_iEOPA7E9FXcZeMt4Ta8AGnrZ000De211N8"
18 );
19
20 \Stripe\Stripe::setApiKey('sk_test_9pOerxcSe3JipvIL2NRCBctF00X2NQT3oi');
21
22 //add customer to stripe
23 $customer = \Stripe\Customer::create(array(
24 'email' => $email,
25 'source' => $source
26 ));
27
28 //item information
29 $itemName = "Zack Location";
30 $itemPrice = $totalamount*100;
31 $currency = "eur";
32 $orderID = (rand(10000000,999999999));
33
34 //charge a credit or a debit card
35 $charge = \Stripe\Charge::create(array(
36 'customer' => $customer->id,
37 'amount' => $itemPrice,
38 'currency' => $currency,
39 'source' => $source
40 ));
41
42
43
44
45 //retrieve charge details
46 $chargeJson = $charge->jsonSerialize();
47
48 //check whether the charge is successful
49 if($chargeJson['amount_refunded'] == 0 && empty($chargeJson['failure_code']) && $chargeJson['captured'] ==1 ) {
50 //order details
51 $amount = $chargeJson['amount'];
52 $balance_transaction = $chargeJson['balance_transaction'];
53 $currency = $chargeJson['currency'];
54 $payment_status = $chargeJson['status'];
55 $source_status = $chargeJson['source']['status'];
56 $stripe_source = $chargeJson['source']['id'];
57 $payment_id = $chargeJson['id'];
58 $client_secret = $chargeJson['source']['client_secret'];
59 $mandate_url = $chargeJson['source']['sepa_debit']['mandate_url'];
60 $response = json_encode($chargeJson);
61
62 //include database config file
63 include_once 'dbConfig.php';
64
65 //insert transaction data into the database
66 $sql = "INSERT INTO zak_sepa_orders(userid,name,email,item_name,currency,amount,payment_status,source_status,stripe_source,payment_id,client_secret,mandate_url,response)VALUES('".$userid."','".$name."','".$email."','".$itemName."','".$currency."','".$amount."','".$payment_status."','".$source_status."','".$stripe_source."','".$payment_id."','".$client_secret."','".$mandate_url."','".$response."')";
67 $insert = $db->query($sql);
68 $last_insert_id = $db->insert_id;
69 //if order inserted successfully
70 if($last_insert_id && $payment_status == 'pending' || $payment_status == 'succeeded') {
71 $statusMsg = "The transaction successful.<h4>Order ID: {$last_insert_id}</h4><a href='$mandate_url' target='_blank'>View Mandate</a>";
72 } else {
73 $statusMsg = "Transaction has been failed";
74 }
75 } else {
76 $statusMsg = "Transaction has been failed";
77 }
78} else {
79 $statusMsg = "Form submission error.....";
80}
81?>
82<!DOCTYPE html>
83<html lang="en">
84<head>
85 <title>Charge</title>
86 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
87</head>
88<body>
89 <div class="container text-center">
90 <h2 class="text-success text-center m-5"><?php echo $statusMsg; ?></h2>
91 <a href="<?php echo site_url();?>/sepa-payment/" class="btn btn-dark btn-lg">Back</a>
92 </div>
93</body>
94</html>