下午好,
我正在实现一个支付处理器,我需要带来相应的支付ID,这是自动生成的后端。
我用PHP创建了前端和数据库,在前端用JS向后端请求数据,但是我无法得到ID。
我尝试通过脚本将php中的id传递给一个js变量,然后用localstorage将其发送到前端,但没有成功。
$payment->transaction_amount = (float) $form_data['transaction_amount'];
$payment->token = $form_data['token'];
$payment->description = "Pago";
$payment->installments = (int)$form_data['installments'];
$payment->payment_method_id = $form_data['payment_method_id'];
$payment->issuer_id = (int)$form_data['issuer_id'];
$payer = new MercadoPago\Payer();
$payer->email = $form_data['payer']['email'];
$payer->identification = array(
"type" => $form_data['payer']['identification']['type'],
"number" => $form_data['payer']['identification']['number']
);
$payer->first_name = $form_data['cardholderName'];
$payment->payer = $payer;
$payment->save();
$response = array(
'status' => $payment->status,
'status_detail' => $payment->status_detail,
'id' => $payment->id
);
$id = ($response['id']); //separating the id
echo ($id); // id to bring to the frontend
JS:
<script>
payId = "<?php echo $id ?>"; //pass the id from php to js
payId = localStorage.setItem('idPago', payId); //I save it with localstorage
</script>
前端:
const renderCardPaymentBrick = async (bricksBuilder) => {
const settings = {
initialization: {
//tenes que fijarte si esto esta en pesos o
amount: price, // monto a ser pago
// preferenceId: "" // where i need to bring the id
},
callbacks: {
onReady: () => {
// callback llamado cuando Brick esté listo
},
onSubmit: (cardFormData) => {
cardFormData['cardholderName'] = document.getElementsByName("HOLDER_NAME")[0].value;
// ejemplo de envío de los datos recolectados por el Brick a su servidor
return new Promise((resolve, reject) => {
fetch("./api/mp.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(cardFormData)
})
.then((response) => {
console.log(response);
resolve();
})
.catch((error) => {
console.log(error);
// tratar respuesta de error al intentar crear el pago
reject();
})
});
},
onError: (error) => {
// callback llamado para todos los casos de error de Brick
},
},
};
const cardPaymentBrickController = await bricksBuilder.create('cardPayment', 'cardPaymentBrick_container', settings);
};
很抱歉,有些注解是西班牙语的,但重要的是前端开头的preferenceId。
我很感激你帮忙!非常感谢。
1条答案
按热度按时间ntjbwcob1#
您可以通过创建隐藏输入来读取其中的数据。例如:
JS: