Telegram Bot对接Stripe支付:pre_checkout_query响应报错求助
排查Telegram支付Bot处理pre_checkout_query时的报错问题
我正在开发对接Stripe的Telegram支付Bot,打算通过Telegram直接处理支付流程,但收到pre_checkout_query并尝试响应时出现报错。以下是我在Apps Script中写的代码,包含测试用的拒绝支付逻辑,以及计划使用的接受支付逻辑,麻烦帮忙排查问题。
if (contenido.pre_checkout_query != null) { tg.enviarMensaje(telegramUrl, idAdri, "dentroif", "HTML") var idoperacion = contenido.pre_checkout_query.id UrlFetchApp.fetch(telegramUrl + '/answerShippingQuery?shipping_query_id =' + idoperacion + '&ok=False' + "&error_message=Test") /*var precios = [ { "label": "Listado Usuarios Karma", "amount": "145" }, { "label": "Listado Usuarios Karma", "amount": "150" } ] var opcionesprecio = [ { "id": idoperacion, "title": "Titulo", "prices": encodeURIComponent(JSON.stringify(precios)) }, ] UrlFetchApp.fetch(telegramUrl + '/answerPreCheckoutQuery?shipping_query_id=' + idoperacion + '&ok=True' + "ShippingOption=" + encodeURIComponent(JSON.stringify(opcionesprecio))) */ }
问题排查与修正要点
- 接口调用错误:当前处理的是
pre_checkout_query,但代码里调用了answerShippingQuery接口——这是处理配送查询的接口,和预支付查询完全不匹配。正确接口应为answerPreCheckoutQuery。 - 参数格式错误:
- 参数名里的空格必须删除,比如
shipping_query_id =要改成shipping_query_id=,否则Telegram服务器无法识别参数。 - 接受支付逻辑里的参数名写错了,
ShippingOption应为shipping_options,且该参数属于answerShippingQuery接口,answerPreCheckoutQuery在ok=true时不需要传递这个参数。
- 参数名里的空格必须删除,比如
- 数据类型错误:金额
amount必须是数字类型,不能用字符串,比如"145"要改成145。 - 编码问题:传递JSON参数时,只需对整个JSON字符串做一次
encodeURIComponent,不需要单独编码内部的prices字段。
修正后的示例代码
拒绝支付的正确逻辑
if (contenido.pre_checkout_query != null) { tg.enviarMensaje(telegramUrl, idAdri, "dentroif", "HTML") var idoperacion = contenido.pre_checkout_query.id // 调用正确的接口,删除参数中的空格 UrlFetchApp.fetch(telegramUrl + '/answerPreCheckoutQuery?pre_checkout_query_id=' + idoperacion + '&ok=False&error_message=Test') }
接受支付的正确逻辑(无需调整配送/价格时)
if (contenido.pre_checkout_query != null) { tg.enviarMensaje(telegramUrl, idAdri, "dentroif", "HTML") var idoperacion = contenido.pre_checkout_query.id // 直接确认预支付查询,ok=true即可 UrlFetchApp.fetch(telegramUrl + '/answerPreCheckoutQuery?pre_checkout_query_id=' + idoperacion + '&ok=True') }
内容的提问来源于stack exchange,提问作者Jesus
相关产品推荐
相关产品推荐

