Update confirm_payment.php

This commit is contained in:
Frédéric FRANCE 2019-04-26 17:57:23 +02:00 committed by GitHub
parent 3e99a40b9e
commit 31022e6753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,14 +97,14 @@ dol_syslog("POST=".var_export($_POST, true));
header('Content-Type: application/json');
# retrieve json from POST body
// retrieve json from POST body
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$intent = null;
try {
if (isset($json_obj->payment_method_id)) {
# Create the PaymentIntent
// Create the PaymentIntent
$intent = \Stripe\PaymentIntent::create([
'payment_method' => $json_obj->payment_method_id,
'amount' => 1099,
@ -121,32 +121,39 @@ try {
}
generatePaymentResponse($intent);
} catch (\Stripe\Error\Base $e) {
# Display error on client
// Display error on client
echo json_encode([
'error' => $e->getMessage()
]);
}
function generatePaymentResponse($intent) {
/*
* generate payment response
*
* @param \Stripe\PaymentIntent $intent PaymentIntent
* @return void
*/
function generatePaymentResponse($intent)
{
if ($intent->status == 'requires_source_action' &&
$intent->next_action->type == 'use_stripe_sdk') {
# Tell the client to handle the action
echo json_encode([
'requires_action' => true,
'payment_intent_client_secret' => $intent->client_secret
]);
} else if ($intent->status == 'succeeded') {
# The payment didnt need any additional actions and completed!
# Handle post-payment fulfillment
// Tell the client to handle the action
echo json_encode([
'requires_action' => true,
'payment_intent_client_secret' => $intent->client_secret
]);
} elseif ($intent->status == 'succeeded') {
// The payment didnt need any additional actions and completed!
// Handle post-payment fulfillment
// TODO
// TODO
echo json_encode([
"success" => true
]);
} else {
# Invalid status
http_response_code(500);
echo json_encode(['error' => 'Invalid PaymentIntent status']);
}
echo json_encode([
"success" => true
]);
} else {
// Invalid status
http_response_code(500);
echo json_encode(['error' => 'Invalid PaymentIntent status']);
}
}