· 6 years ago · Aug 04, 2019, 09:30 AM
1<?php
2require_once('stripe-php/init.php');
3$stripe = [
4 "secret_key" => "",
5 "publishable_key" => "p",
6];
7
8\Stripe\Stripe::setApiKey($stripe['secret_key']);
9
10header('Content-Type: application/json');
11# retrieve json from POST body
12$json_str = file_get_contents('php://input');
13$json_obj = json_decode($json_str);
14
15$intent = null;
16
17try {
18 if (isset($json_obj->payment_method_id)) {
19 # Create the PaymentIntent
20 $intent = \Stripe\PaymentIntent::create([
21 'payment_method' => $json_obj->payment_method_id,
22 'amount' => $json_obj->preciostripe,
23 'description' => $json_obj->description,
24 'currency' => 'eur',
25 'confirmation_method' => 'manual',
26 'confirm' => true,
27 ]);
28 }
29 if (isset($json_obj->payment_intent_id)) {
30 $intent = \Stripe\PaymentIntent::retrieve(
31 $json_obj->payment_intent_id
32 );
33 //$payfees = \Stripe\BalanceTransaction::retrieve($json_obj->payment_intent_id);
34 //$fee = $payfees->fee;
35 $intent->confirm();
36 }
37 return generatePaymentResponse($intent, [
38 'language' => $json_obj->language,
39 'telmobile' => $json_obj->telmobile,
40 'cliente' => $json_obj->cliente,
41 'emailtrue' => $json_obj->emailtrue,
42 ]);
43}
44catch (\Stripe\Error\Base $e) {
45 # Display error on client
46 echo json_encode([
47 'error' => $e->getMessage()
48 ]);
49}
50
51
52function generatePaymentResponse($intent, $params = []) {
53 # Note that if your API version is before 2019-02-11, 'requires_action'
54 # appears as 'requires_source_action'. Ojo, cambiar tambien en script cliente
55 if ($intent->status == 'requires_source_action' &&
56 $intent->next_action->type == 'use_stripe_sdk') {
57 # Tell the client to handle the action
58 echo json_encode([
59 'requires_source_action' => true,
60 'payment_intent_client_secret' => $intent->client_secret
61 ]);
62 } else if ($intent->status == 'succeeded') {
63 # The payment didn’t need any additional actions and completed!
64 # Handle post-payment fulfillment
65 include '../paypal/conex/conexionpdo.php';
66 $status = $intent->status;
67 $id = $intent->id;
68 $description = $intent->description;
69 $amount = $intent->amount;
70 $amountbase=number_format(($amount/100),2);
71 $date = date("Y-m-d H:i:s");
72
73 $query = $dbh->prepare("insert into orders_excursiones (language, telmobile, concepto, holidaycost, statement, txn_id, mc_gross,
74 name, emailtrue, orderdatetime)
75 VALUES (:language, :telmobile, :description, :amountbase, :status, :id, :amountbase2, :cliente, :emailtrue, :date)");
76
77 $query->execute(array(
78 'language' => $params['language']
79 , 'telmobile' => $params['telmobile']
80 , 'description' => $description
81 , 'amountbase' => $amountbase
82 , 'status' => $status
83 , 'id' => $id
84 , 'amountbase2' => $amountbase
85 , 'cliente' => $params['cliente']
86 , 'emailtrue' => $params['emailtrue']
87 , 'date' => $date
88 ));
89 echo json_encode([
90 "success" => true
91 ]);
92 } else {
93 # Invalid status
94 http_response_code(500);
95 echo json_encode(['error' => 'Invalid PaymentIntent status']);
96 }
97}