What I'm trying to do is call function form server and validate data in javascript function but the return statement from server make redirect before response.complete("success")
button.onclick = async function handlePurchase() {
const payment = new PaymentRequest(methods, details, options);
try {
const response = await payment.show();
// Call server logic here and validate response if OK continue
// But server response redirect here so code not completed
$('#checkout-form').submit();
$.ajax({
url: '{{route("apple-pay")}}',
type: 'post',
data: $('#checkout-form').serialize(),
success: function(data){
console.log('data :>> ', data);
}
});
await response.complete("success");
// redirect to success page
} catch (err) {
console.error("Uh oh, something bad happened", err.message);
}
}
Server function:
public function pay(Request $request)
{
$merchant_id = env('CREDIMAX_MERCHANT_ID');
$password = env('CREDIMAX_INTEGRATION_PASSWORD');
$response = Http::withBasicAuth('merchant.'.$merchant_id, $password)->put
('https://example.com/api/rest/merchant/'.$merchant_id.'/order/1530/transaction/1', $data);
return $respone->json();
}
CodePudding user response:
Seperate the request you are making to the third party app and the response you are sending back to you ajax call. This is how I mean:
public function pay(Request $request)
{
$merchant_id = env('CREDIMAX_MERCHANT_ID');
$password = env('CREDIMAX_INTEGRATION_PASSWORD');
$response = Http::withBasicAuth('merchant.'.$merchant_id, $password)->put
('https://example.com/api/rest/merchant/'.$merchant_id.'/order/1530/transaction/1', $data);
return response()->json([
'success' => $response->ok() ? 1 : 0,
...
]);
}
CodePudding user response:
Check the last line in the controller it says "return $respone->json();" and should be "return $response->json();" -- missing the "s" in response.