Таким образом, проблема с моим кодом заключается в том, что мне не нужно обращаться к www.website.com/webhooks.php, чтобы получить мой ответ, вместо этого я должен сохранить ответ в базе данных или файле или отправить его куда-нибудь так:
<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey('sk_test_xxxx');
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
print_r($payload);
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
//echo 'success';
// Then define and call a method to handle the successful payment intent.
$msg='success';
handlePaymentIntentSucceeded($paymentIntent,$msg);
break;
case 'payment_intent.payment_failed':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
$msg='failed';
handlePaymentIntentSucceeded($paymentMethod,$msg);
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached($paymentMethod);
break;
case 'payment_intent.canceled':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
$msg='canceled';
handlePaymentIntentSucceeded($paymentMethod,$msg);
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached($paymentMethod);
break;
// ... handle other event types
case 'payment_intent.created':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
$msg='created';
handlePaymentIntentSucceeded($paymentMethod,$msg);
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached($paymentMethod);
break;
case 'payment_intent.processing':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
$msg='processing';
handlePaymentIntentSucceeded($paymentMethod,$msg);
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached($paymentMethod);
break;
default:
// Unexpected event type
http_response_code(400);
exit();
}
http_response_code(200);
function handlePaymentIntentSucceeded($paymentIntent,$status){
$data=[
'amount'=>$paymentIntent->amount,
'name'=>$paymentIntent->charges->data[0]->billing_details->name,
'email'=>$paymentIntent->charges->data[0]->billing_details->email,
'error'=>$paymentIntent->last_payment_error->message,
'status'=>$status
];
$date=date("Y/m/d");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
// db credentials
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'database');
// Connect with the database.
function connect()
{
$connect = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno($connect)) {
die("Failed to connect:" . mysqli_connect_error());
}
mysqli_set_charset($connect, "utf8");
return $connect;
}
$con = connect();
// Store data into a database .
$sql = "INSERT INTO `ordre`( `amount`, `email`, `name`, `status`, `msg`, `date`) VALUES ({$data['amount']},'{$data['email']}','{$data['name']}','{$data['status']}',\"{$data['error']}\",'{$date}')";
if(mysqli_query($con,$sql))
{
http_response_code(201);
//to check if the storage process is done and
$myFile = "https://6537bee0.ngrok.io/file.txt";
$myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
$newnbord ='done';
fwrite($myFileLink2, $newnbord);
fclose($myFileLink2);
return $newnbord ;
}
else
{ http_response_code(422);
// write error code in a file
http_response_code(201);
$myFile2 = "https://6537bee0.ngrok.io/file.txt";
$myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
$newnbord =mysqli_error($con).$sql
;
fwrite($myFileLink2, $newnbord);
fclose($myFileLink2);
return $newnbord ;
}
}