Coldfusion CFHTTP 访问 API Openpay.mx 意外字符('m'(代码 109)):需要一个有效值

Coldfusion CFHTTP access API Openpay.mx Unexpected character ('m' (code 109)): expected a valid value

我在使用 API OpenPay.mx(支付墨西哥银行和其他支付服务)时遇到了一些问题。这个APIreturnsJSON。我尝试使用 CFHTTP 访问 API,但它返回 HTTP 400 错误请求错误。

完全错误:

https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges {"category":"request","description":"Unexpected character ('m' (code 109)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')","http_code":400,"error_code":1001,"request_id":"79a19194-61a2-49e5-8cbc-c83f5c93ce69"}

在 API 帮助的错误列表中,关于错误 1001-400,这是解释:

The request format is JSON, the fields do not have the correct format, or the request does not have fields that are required.

ColdFusion 代码:

 <cfset request_id = "sk_722a9645ea0040899ccd1f0a53dfcf53">
 <cfset method="store">
 <cfset amount=100>
 <cfset description="Cargo con tienda">
 <cfset customer="Gabriel Villafuerte">

 <cfhttp url="https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges"
 method="post" charset="utf-8"  username="#request_id#" password=""
 throwonerror="no">
      <cfhttpparam type="header" name="Content-Type" value="application/json"/>
      <cfhttpparam name="method" type="formfield" value="#method#">
      <cfhttpparam name="amount" type="FormField" value="#amount#">
      <cfhttpparam name="description" type="FormField" value="#description#">
      <cfhttpparam name="customer" type="FormField" value="#customer#">
 </cfhttp>

<CFDUMP var="#cfhttp#">

<!---display results--->

<cfoutput>
    HTTP Response = #cfhttp.statusCode# <br>
       <textarea cols=80 rows=10>
           https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges
             #cfhttp.fileContent#
       </textarea>
 </cfoutput>

这些是访问规则 OpenPay.mx API:

有人知道我如何给出正确的字段格式吗?

我不熟悉 API,但查看文档和示例,我怀疑错误消息的意思正是它所说的(强调我的)。

The format of the request is not JSON, the fields do not have the correct format, OR the request does not have fields that are required.

请求值必须作为 JSON 提交,而不是单独的字段,并且显然必须包含所有必需的值。不使用 "formfield",而是将值放在结构中。使用 serializeJSON() 将其转换为 JSON。然后使用参数类型 "body" 将 JSON 传递给 API。

无论调用哪种方法,您都需要查看 API 示例,以确定需要哪些参数。然而,Charges via Store Example 做了一些细微的修改:

  • "due_date" 不能是过去的日期
  • "order_id" 必须是尚未处理的值。 (我只是将样本数量增加了任意数量,直到我找到一个有效的 id。)

通过商店示例收费

<!--- sample request from API --->
<!--- note: increased "order_id" value by arbitrary amount --->
<cfset timeNow = now()>                 
<cfset requestData = {
   "method" : "store",
   "amount" : 100,
   "description" : "Cargo con tienda",
   "order_id" : "oid-00100",
   "due_date" : dateFormat(timeNow, "yyyy-mm-dd")&"T"&timeFormat(timeNow, "HH:nn:ss")
}>

 <cfhttp url="https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/charges"
    method="post" 
    charset="utf-8"  
    username="sk_e568c42a6c384b7ab02cd47d2e407cab:" 
    password=""
    throwonerror="no">
      <cfhttpparam type="header" name="Content-Type" value="application/json"/>
      <cfhttpparam type="body" value="#serializeJSON(requestData)#">
 </cfhttp>

 <cfdump var="#cfhttp#">

结果: