· 6 years ago · Aug 03, 2019, 08:50 AM
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# 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//$payfees =null;
17try {
18 if (isset($json_obj->language)){
19 // $language = $json_obj->language;
20 if (isset($json_obj->payment_method_id)) {
21 //$language = $json_obj->language;
22 # Create the PaymentIntent
23 $intent = \Stripe\PaymentIntent::create([
24 'payment_method' => $json_obj->payment_method_id,
25 'amount' => $json_obj->preciostripe,
26 'description' => $json_obj->description,
27 'currency' => 'eur',
28 'confirmation_method' => 'manual',
29 'confirm' => true,
30 ]);
31 }
32 if (isset($json_obj->payment_intent_id)) {
33 $intent = \Stripe\PaymentIntent::retrieve(
34 $json_obj->payment_intent_id
35 );
36 //$payfees = \Stripe\BalanceTransaction::retrieve($json_obj->payment_intent_id);
37 //$fee = $payfees->fee;
38 $intent->confirm();
39 //$intent->confirm(['expand' => 'charges.data.balance_transaction']);
40 //$intent->confirm(['expand' => 'balance_transaction.fee']);
41 // $intent->confirm(["expand" => array("balance_transaction")]);
42 }
43 generatePaymentResponse($intent);
44}
45}
46catch (\Stripe\Error\Base $e) {
47 # Display error on client
48 echo json_encode([
49 'error' => $e->getMessage()
50 ]);
51}
52
53function generatePaymentResponse($intent) {
54 # Note that if your API version is before 2019-02-11, 'requires_action'
55 # appears as 'requires_source_action'. Ojo, cambiar tambien en script cliente
56 if ($intent->status == 'requires_source_action' &&
57 $intent->next_action->type == 'use_stripe_sdk') {
58 # Tell the client to handle the action
59 echo json_encode([
60 'requires_source_action' => true,
61 'payment_intent_client_secret' => $intent->client_secret
62 ]);
63 } else if ($intent->status == 'succeeded') {
64 # The payment didn’t need any additional actions and completed!
65 # Handle post-payment fulfillment
66 include '../paypal/conex/conexionpdo.php';
67 $status = $intent->status;
68 $id = $intent->id;
69 $amount = $intent->amount;
70 // $language = $json_obj->language;
71 $language = "no";
72 $telmobile = "test";
73 //$language = "no";
74 $sql = "INSERT INTO orders_excursiones(language, telmobile,statement, txn_id, mc_gross)
75 VALUES('".$language."','".$telmobile."','".$status."','".$id."','".$amount."')";
76 $insert = $dbh->query($sql);
77 echo json_encode([
78 "success" => true
79 ]);
80 } else {
81 # Invalid status
82 http_response_code(500);
83 echo json_encode(['error' => 'Invalid PaymentIntent status']);
84 }
85}