Coldfusion CFHTTP访问API Openpay.mx意外字符(' m'(代码109)):预期有效值

时间:2016-05-27 22:38:19

标签: json api coldfusion coldfusion-9 cfhttp

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

完成错误:

  

https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges   {"类别":"请求","说明":"意外的字符(' m'(代码   109)):期望一个有效值(数字,字符串,数组,对象,'真',   '假'要么   '空')"" HTTP_CODE" 400" ERROR_CODE":1001," REQUEST_ID":" 79a19194 -61a2-49e5-8cbc-c83f5c93ce69"}

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

  

请求格式为JSON,字段格式不正确,   或者请求没有所需的字段。

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的规则: OpenPay.mx Rules

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

1 个答案:

答案 0 :(得分:2)

我不熟悉API,但查看文档和示例,我怀疑错误消息的确意味着它所说的内容(强调我的)。

  

请求的格式不是JSON ,字段没有   格式正确,或者请求没有所需的字段。

请求值必须作为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#">

<强>结果:

OpenPay.mx Response