· 6 years ago · Dec 16, 2019, 08:00 PM
1function payment_attempt( $data ) {
2 stripe_init(); // Require Stripe API, Set API KEY
3 try {
4 $charge = \Stripe\Charge::create([
5 "amount" => $data['end_value'],
6 "billing_details" => array(
7 "address" => array(
8 "city" => $data['city'],
9 "country" => 'Canada',
10 "line1" => $data['address'],
11 "line2" => $data['unit'],
12 "postal_code" => $data['zip'],
13 "state" => 'Québec',
14 ),
15 "email" => $data['email'],
16 "name" => $data['name'],
17 "phone" => $data['phone'],
18 ),
19 "currency" => "cad",
20 "description" => $data['desc'],
21 "source" => $data['token'],
22 ]);
23 // That's it! You're done! The payment was processed
24 echo json_encode($charge);
25 } catch (\Stripe\Error\Card $e) {
26 // Handle "hard declines" e.g. insufficient funds, expired card, etc
27 // See https://stripe.com/docs/declines/codes for more
28 echo json_encode(["error"=> $e->getMessage()]);
29 }
30}
31
32 // payment info
33 $product = $_POST['product'];
34 $quantity = $_POST['quantity'];
35
36 if ( $product == 'ticket' ) {
37 $desc = 'Billets Évasion Lanaudière';
38 $value = $quantity * 2500;
39 }
40
41 $tps = $value * 0.05;
42 $tvq = $value * 0.09975;
43 $end_value = $value + $tps + $tvq;
44
45 // Array of data
46 $data = array(
47 'first_name' => $_POST['first_name'];,
48 'last_name' => $_POST['last_name'];,
49 'name' => "$first_name $last_name",
50 'email' => $_POST['email'],
51 'phone' => $_POST['phone'],
52 'delivery' => $_POST['delivery'],
53 'address' => $_POST['address'],
54 'unit' => $_POST['unit'],
55 'city' => $_POST['city'],
56 'zip' => $_POST['zip'],
57 'product' => $product,
58 'quantity' => $quantity,
59 'desc' => $desc,
60 'value' => $value,
61 'tps' => round( $tps ),
62 'tvq' => round( $tvq ),
63 'end_value' => round( $end_value ),
64 'token' => $_POST['stripeToken']
65 );
66
67payment_attempt( $data );