· 6 years ago · Jul 22, 2019, 11:18 AM
1<?php
2//check whether stripe token is not empty
3if(!empty($_POST['stripeToken'])){
4$token = $_POST['stripeToken'];
5$stripe = [
6 "secret_key" => "my key",
7 "publishable_key" => "my key",
8 ];
9 \Stripe\Stripe::setApiKey($stripe['secret_key']);
10
11 //add customer to stripe
12 $customer = \Stripe\Customer::create(array(
13 'email' => $email,
14 //'source' => $token
15 'payment_method' => $token
16 ));
17 //item information
18 $itemName = $excursion." ".$fecha." ".$cliente;
19 $itemPrice = $preciostripe;
20 $currency = "eur";
21 $orderID = $idreserva;
22
23 //charge a credit or a debit card
24 $intent = \Stripe\PaymentIntent::create(array(
25 'customer' => $customer->id,
26 'amount' => $itemPrice,
27 'currency' => $currency,
28 'description' => $itemName,
29 'metadata' => array(
30 'order_id' => $orderID
31 )
32 ));
33 //retrieve charge details
34
35function generatePaymentResponse($intent) {
36 if ($intent->status == 'requires_source_action' &&
37 $intent->next_action->type == 'use_stripe_sdk') {
38 # Tell the client to handle the action
39 echo json_encode([
40 'requires_action' => true,
41 'payment_intent_client_secret' => $intent->client_secret
42 ]);
43 } else if ($intent->status == 'succeeded') {
44 # The payment didn’t need any additional actions and completed!
45 # Handle post-payment fulfillment
46 echo json_encode([
47 'success' => true
48 ]);
49 } else {
50 # Invalid status
51 http_response_code(500);
52 echo json_encode(['error' => 'Invalid PaymentIntent status']);
53 }
54}
55generatePaymentResponse($intent);
56}
57?>