Adolfo
Adolfo

Reputation: 131

Acessing array inside form variable sent

I`m working with PayPal and I need this block of code to read the post Payment Responses of API:

<cfif StructKeyExists(FORM.TRANSACTION[0],"ID_FOR_SENDER_TXN")>
    <cfset pTxnId = FORM.TRANSACTION[0].ID_FOR_SENDER_TXN />
</cfif>

But when I runs the payment response test tool, I got this error: 500 Element TRANSACTION is undefined in a Java object of type class [Ljava.lang.String;.

I used this loop:

<cfloop collection="#form#" item="theField">
        <cfif theField is not "fieldNames">
            #theField# = #form[theField]#<br>
        </cfif>
    </cfloop>

to see the variables sent in FORM and the variables is like this:

I don't know why I can't access this.

(Update from comments)

Tried use this code:

<cfif isDefined("form['FORM.TRANSACTION[0].ID_FOR_SENDER_TXN']")> 
     <cfset pTxnId = form['FORM.TRANSACTION[0].ID_FOR_SENDER_TXN'] />  
</cfif> 

Now I am getting the error:

500 Parameter 1 of function IsDefined, which is now form['FORM.TRANSACTION[0].ID_FOR_SENDER_TXN'], must be a syntactically valid variable name.

Same error if I use

form['FORM.TRANSACTION[0].ID_FOR_SENDER_TXN']

or

form['TRANSACTION[0].ID_FOR_SENDER_TXN']

Update 2

Worked with:

<cfif structKeyExists(FORM, "TRANSACTION[0].ID_FOR_SENDER_TXN")>

</cfif>

Thank you!

Upvotes: 2

Views: 129

Answers (1)

Adam Tuttle
Adam Tuttle

Reputation: 19824

If the form field name is TRANSACTION[0].ID_FOR_SENDER_TXN (which is what your debugging loop is indicating), then you should address it as:

form['FORM.TRANSACTION[0].ID_FOR_SENDER_TXN']

To clarify:

When you access a variable using this notation:

<cfset pTxnId = FORM.TRANSACTION[0].ID_FOR_SENDER_TXN />

ColdFusion expects this data structure:

http://note.io/15v3o51

It looks for a key in the form named transaction, and tries to get the 0th index from it (side note: this wouldn't work in CF anyway, arrays begin with index 1), and as that first item in the array, it's expecting a structure with a key named ID_FOR_SENDER_TXN.

However, what PayPal is sending you is actually using this format:

http://note.io/19nTSZR

This explains the error message: "Element TRANSACTION is undefined in a Java object of type class [Ljava.lang.String;."

Upvotes: 7

Related Questions