· 6 years ago · Aug 02, 2019, 09:58 PM
1<?php
2require_once('stripe-php/init.php');
3$stripe = [
4 "secret_key" => "",
5 "publishable_key" => "",
6];
7
8\Stripe\Stripe::setApiKey($stripe['secret_key']);
9
10header('Content-Type: application/json');
11$json_str = file_get_contents('php://input');
12$json_obj = json_decode($json_str);
13
14$intent = null;
15try {
16 if (isset($json_obj->payment_method_id)) {
17 $intent = \Stripe\PaymentIntent::create([
18 'payment_method' => $json_obj->payment_method_id,
19 'amount' => $json_obj->preciostripe,
20 'description' => $json_obj->description,
21 'currency' => 'eur',
22 'confirmation_method' => 'manual',
23 'confirm' => true,
24 ]);
25 }
26 if (isset($json_obj->payment_intent_id)) {
27 $intent = \Stripe\PaymentIntent::retrieve(
28 $json_obj->payment_intent_id
29 );
30 $intent->confirm();
31 }
32 generatePaymentResponse($intent);
33} catch (\Stripe\Error\Base $e) {
34 echo json_encode([
35 'error' => $e->getMessage()
36 ]);
37}
38
39function generatePaymentResponse($intent) {
40 if ($intent->status == 'requires_source_action' &&
41 $intent->next_action->type == 'use_stripe_sdk') {
42 echo json_encode([
43 'requires_source_action' => true,
44 'payment_intent_client_secret' => $intent->client_secret
45 ]);
46 } else if ($intent->status == 'succeeded') {
47 $status = $intent->status;
48 $id = $intent->id;
49 $amount = $intent->amount;
50 $sql = "INSERT INTO orders_excursiones(statement, txn_id, mc_gross)
51 VALUES('".$status."','".$id."','".$amount."')";
52 $insert = $dbh->query($sql);
53 echo json_encode([
54 "success" => true
55 ]);
56 } else {
57 # Invalid status
58 http_response_code(500);
59 echo json_encode(['error' => 'Invalid PaymentIntent status']);
60 }
61}