· 6 years ago · Aug 04, 2019, 07:08 PM
1<?php
2require_once('stripe-php/init.php');
3$stripe = [
4 "secret_key" => "",
5 "publishable_key" => "a",
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 $intent = \Stripe\BalanceTransaction::retrieve([
34 'id' => $json_obj->payment_method_id,
35 'expand' => ['PaymentIntent']
36]);
37 $intent->confirm();
38 }
39 return generatePaymentResponse($intent, [
40 'language' => $json_obj->language,
41 'telmobile' => $json_obj->telmobile,
42 'cliente' => $json_obj->cliente,
43 'emailtrue' => $json_obj->emailtrue,
44 ]);
45}
46catch (\Stripe\Error\Base $e) {
47 # Display error on client
48 echo json_encode([
49 'error' => $e->getMessage()
50 ]);
51}
52
53
54function generatePaymentResponse($intent, $params = []) {
55 //function generatePaymentResponse($intent) {
56 # Note that if your API version is before 2019-02-11, 'requires_action'
57 # appears as 'requires_source_action'. Ojo, cambiar tambien en script cliente
58 if ($intent->status == 'requires_source_action' &&
59 $intent->next_action->type == 'use_stripe_sdk') {
60 # Tell the client to handle the action
61 echo json_encode([
62 'requires_source_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 include '../paypal/conex/conexionpdo.php';
69 $status = $intent->status;
70 $id = $intent->id;
71 $description = $intent->description;
72 $amount = $intent->amount;
73 $fee = $intent->fee;
74 $amountbase=number_format(($amount/100),2);
75 $date = date("Y-m-d H:i:s");
76
77 $query = $dbh->prepare("insert into orders_excursiones (language, telmobile, mc_fee, concepto, holidaycost, statement, txn_id, mc_gross,
78 name, emailtrue, orderdatetime)
79 VALUES (:language, :telmobile, :fee, :description, :amountbase, :status, :id, :amountbase2, :cliente, :emailtrue, :date)");
80
81 $query->execute(array(
82 'language' => $params['language']
83 , 'telmobile' => $params['telmobile']
84 , 'fee' => $fee
85 , 'description' => $description
86 , 'amountbase' => $amountbase
87 , 'status' => $status
88 , 'id' => $id
89 , 'amountbase2' => $amountbase
90 , 'cliente' => $params['cliente']
91 , 'emailtrue' => $params['emailtrue']
92 , 'date' => $date
93 ));
94 echo json_encode([
95 "success" => true
96 ]);
97 } else {
98 # Invalid status
99 http_response_code(500);
100 echo json_encode(['error' => 'Invalid PaymentIntent status']);
101 }
102}
103?>