rembo
January 2, 2022, 6:47am
1
Hello ,
I am try to send post data to stripe using this method :
// Create a Checkout Session with the selected product
const createCheckoutSession = function (stripe) {
return fetch("<?= BasePath()?>/api/CheackCarda?productName=Codex_Demo_Product&&productID=DP12345&&productPrice=55&¤cy=usd" ,{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
createCheckoutSession: 1,
}),
}).then(function (result) {
console.log(result)
return result.json();
});
};
but the data not passed to the app routes for prosses the action I got only ‘null’ data in the router response , any idea ?
Thanx
arbei
January 2, 2022, 10:27am
2
Make sure your URL is correct, since you use <?= BasePath()?>/api/CheackCarda, you post to your own site.If “CheackCarda” is your custom API action, you may post your code for discussion.
rembo
January 2, 2022, 3:34pm
3
The bath is correct , her is the API part :
$app->post('/CheackCarda[/{params:.*}]', function ($request, $response, array $args) {
$GLOBALS["Request"] = $request;
$productName = $request->getParsedBodyParam('productName');
$productID = $request->getParsedBodyParam('productID');
$productPrice = $request->getParsedBodyParam('productPrice');
$currency = $request->getParsedBodyParam('currency');
if($productName != null && $productID != null){
// Set API key
\Stripe\Stripe::setApiKey(Config("STRIPE_API_KEY"));
$response = array(
'status' => 0,
'error' => array(
'message' => 'Invalid Request!'
)
);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$input = file_get_contents('php://input');
$request = json_decode($input);
}
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
return $response->withJson($response);
exit;
}
if(!empty($request->createCheckoutSession)){
// Convert product price to cent
$stripeAmount = round($productPrice*100, 2);
// Create new Checkout Session for the order
try {
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price_data' => [
'product_data' => [
'name' => $productName,
'metadata' => [
'pro_id' => $productID
]
],
'unit_amount' => $stripeAmount,
'currency' => $currency,
],
'quantity' => 1,
'description' => $productName,
]],
'mode' => 'payment',
'success_url' => Config("STRIPE_SUCCESS_URL").'?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => Config("STRIPE_CANCEL_URL"),
]);
} catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $checkout_session){
$response = array(
'status' => 1,
'message' => 'Checkout Session created successfully!',
'sessionId' => $checkout_session->id
);
}else{
$response = array(
'status' => 0,
'error' => array(
'message' => 'Checkout Session creation failed! '.$api_error
)
);
}
}
// Return response
return $response->withJson($response);
}else{
$response = array(
'status' => 0,
'error' => array(
'message' => 'Checkout Session creation failed! '.$api_error
)
);
return $response->withJson($response);
}
});
rembo
January 3, 2022, 2:06am
5
Perfect it’s work , highly thanx …