Search code examples
jsonapicoldfusioncoldfusion-9cfhttp

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


I'm having some problems using the API OpenPay.mx (payments Mexican Banks & other payments services). This API returns JSON. I'm try to access the API with CFHTTP, but it is returning an HTTP 400 Incorrect request error.

Complete error:

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"}

In the error list from the API help about errors 1001-400, this is the explanation:

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

ColdFusion Code:

 <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>

These are the rules for the access OpenPay.mx API: OpenPay.mx Rules

Does anyone have a clue about how I can give the correct format of the fields?


Solution

  • I am not familiar with the API, but looking over the documentation and examples, I suspect the error message means exactly what it says (emphasis mine).

    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.

    The request values must be submitted as JSON, not separate fields, and obviously must contain all of the required values. Instead of using "formfield", put the values in a structure. Convert it into a JSON using serializeJSON(). Then pass the JSON to the API using parameter type "body".

    You will need to review the API examples, for whichever method you are invoking, to figure out which parameters are required. However, the Charges via Store Example worked with some slight modifications:

    • The "due_date" cannot be a past date
    • The "order_id" must be a value not already processed. (I just increased the sample number by an arbitrary amount until I hit a valid id.)

    Charges via Store Example

    <!--- 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#">
    

    Result:

    OpenPay.mx Response