· 6 years ago · Jul 26, 2019, 08:14 PM
1<?php
2// Only needed if on another domain. I needed to do this for testing.
3header("Access-Control-Allow-Origin: *");
4header("Access-Control-Allow-Headers: *");
5header('Content-Type: application/json');
6header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
7
8require_once('stripe-php/init.php');
9
10
11$stripe = [
12 "secret_key" => "sk_test_",
13 "publishable_key" => "pk_test_",
14];
15
16\Stripe\Stripe::setApiKey($stripe['secret_key']);
17
18$preciostripe = $_POST['preciostripe'];
19$token = $_POST['stripeToken'];
20$name = $_POST['namecard'];
21
22# retrieve json from POST body
23$json_str = file_get_contents('php://input');
24$json_obj = json_decode($json_str);
25$intent = null;
26try {
27 if (isset($json_obj->payment_method_id)) {
28 # Create the PaymentIntent
29 $intent = \Stripe\PaymentIntent::create([
30 'payment_method' => $json_obj->payment_method_id,
31 'amount' => 1099,
32 'currency' => 'usd',
33 'confirmation_method' => 'manual',
34 'confirm' => true,
35 ]);
36 }
37 if (isset($json_obj->payment_intent_id)) {
38 $intent = \Stripe\PaymentIntent::retrieve(
39 $json_obj->payment_intent_id
40 );
41 $intent->confirm();
42 }
43 // generatePaymentResponse($intent);
44 echo json_encode($intent);
45} catch (\Stripe\Error\Base $e) {
46 # Display error on client
47 echo json_encode([
48 'error' => $e->getMessage()
49 ]);
50}
51// function generatePaymentResponse($intent) {
52// # Note that if your API version is before 2019-02-11, 'requires_action'
53// # appears as 'requires_source_action'.
54// ob_start();
55// var_dump($intent);
56// error_log(ob_get_clean(), 4);
57//
58// if ($intent->status == 'requires_action' &&
59// $intent->next_action->type == 'use_stripe_sdk') {
60// # Tell the client to handle the action
61// echo json_encode([
62// 'requires_action' => true,
63// 'payment_intent_client_secret' => $intent->client_secret
64// ]);
65// } else if ($intent->status == 'succeeded') {
66// # The payment didn’t need any additional actions and completed!
67// # Handle post-payment fulfillment
68// echo json_encode([
69// "success" => true
70// ]);
71// } else {
72// # Invalid status
73// http_response_code(500);
74// echo json_encode(['error' => 'Invalid PaymentIntent status']);
75// }
76// }
77
78
79//check whether stripe token is not empty
80// if(!empty($_POST['stripeToken'])){
81//
82// //get token, card and user info from the form
83// $preciostripe = $_POST['preciostripe'];
84// $token = $_POST['stripeToken'];
85// $name = $_POST['namecard'];
86// //include Stripe PHP library
87// $stripe = [
88// "secret_key" => "sk_test_SBNykmhudnlfe8ccpKfNtA9E00bcqB4TOp",
89// "publishable_key" => "pk_test_9X75ivLekg65a5rlcxEb4kZS00gL2A82l9",
90// ];
91// \Stripe\Stripe::setApiKey($stripe['secret_key']);
92//
93// //add customer to stripe
94// $customer = \Stripe\Customer::create(array(
95// 'email' => "test@test.com",
96// 'source' => $token
97// ));
98//
99// //charge a credit or a debit card
100// $charge = \Stripe\PaymentIntent::create(array(
101// 'customer' => $customer->id,
102// 'amount' => $preciostripe,
103// 'currency' => "eur",
104// 'confirm' => true,
105// 'confirmation_method' => 'manual',
106// 'description' => "description here",
107// 'metadata' => array(
108// 'order_id' => "idbooking here"
109// )
110// ));
111
112
113 // //retrieve charge details
114 // $chargeJson = $charge->jsonSerialize();
115 //
116 // //check whether the charge is successful
117 // if($chargeJson['amount_refunded'] == 0 && empty($chargeJson['failure_code']) && $chargeJson['paid'] == 1 && $chargeJson['captured'] == 1){
118 // //order details
119 // $amount = $chargeJson['amount'];
120 // $balance_transaction = $chargeJson['balance_transaction'];
121 // $currency = $chargeJson['currency'];
122 // $status = $chargeJson['status'];
123 //
124 // //if order inserted successfully
125 // if($status == 'succeeded'){
126 // echo json_encode(['message' => "Do Whatever here"]);
127 // } else {
128 // echo json_encode(['message' => "not succeeded"]);
129 // }
130 // }else{
131 // echo json_encode(['message' => "another else block?"]);
132 //
133 // }
134// }