Home > Net >  Amazon Pay Checkout, java, V2 in coldfusion cfc - method not found
Amazon Pay Checkout, java, V2 in coldfusion cfc - method not found

Time:07-08

I am trying to implement Amazon Pay Checkout, V2 in coldfusion (ACF2016/windows) leveraging the provided JAVA code from Amazon's docs and Amazon's .jar file. (https://github.com/amzn/amazon-pay-api-sdk-java#readme)

I have written a cfc, meant to be used in the session. I instantiate the WebstoreClient object, set the Pay Configuration and generate the button code just fine. Click the button on the page, a window opens, log into sandbox, close the window and go to the second checkout page with a sessionid in the URL (keeping it simple) When the button is clicked, amazon creates your checkout session, and all the local methods are GET/POST/PATCH operations to amazon servers - simple enough. Not knowing java, but able to [usually] follow code, it sure looks like everything is there..

Here is where I am having trouble. I cannot getCheckoutSession(sessionid) The error I get is

The getCheckoutSession method was not found. Either there are no methods with the specified method name and argument types or the getCheckoutSession method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.

My first checkout page is like this, works fine.

<cfset session.amazonPayClient=createObject('component', 'cfc.amazon').init(dsn='xyz',env='dev')>
<cfoutput>#session.amazonPayClient.renderButton()#</cfoutput>

The second page has this and is where I get the error - method not found

<cfset session.amazonPayClient.getCheckoutSess(url.amazonCheckoutSessionId)>

This is how I am creating the object - it happens during init() of the cfc

<cffunction name="setClient" access="private" returntype="void" hint="this creates the client used to initialize the checkout" output="no">
        <cfset region=createObject("java", "com.amazon.pay.api.types.Region")>
        
        <cffile action="read" file="#variables.PEM_PATH#" variable="pem">
        <cfset variables.oPayConfiguration = createObject("java", "com.amazon.pay.api.PayConfiguration")
            .setPublicKeyId("#variables.amazonProperties.publicKeyID#")
            .setPrivateKey("#pem#")
            .setRegion(Region.NA)
            .setEnvironment(variables.Environment.SANDBOX)
        >
        
        <cfset variables.oClient=createObject("java", "com.amazon.pay.api.WebstoreClient").init(variables.oPayConfiguration)>
</cffunction>

These 2 work with the button signature - which I assume is used elsewhere (eventually)

<cffunction name="genButtonSignature" access="private" returntype="string" hint="this generates the button signature used to create the checkoutsession" output="no">
    <cfset signature = variables.oClient.generateButtonSignature(variables.amazonProperties.buttonPayload)>
    <cfreturn trim(signature)>
</cffunction>

<cffunction name="getButtonSignature" access="private" returntype="string" output="no">
    <cfreturn trim(variables.amazonProperties.buttonSignature)>
</cffunction>

This renders the button - works just fine

<cffunction name="renderButton" access="public" returntype="string" output="no">
    <cfset var buttonCode="">
    <cfsavecontent variable="buttonCode">
        <div id="AmazonPayButton"></div>
        <script src="<cfoutput>#variables.amazonProperties.payScript#</cfoutput>"></script>
        <script type="text/javascript" charset="utf-8">
              amazon.Pay.renderButton('#AmazonPayButton', {
                   // set checkout environment
                   merchantId: '<cfoutput>#variables.amazonProperties.merchantID#</cfoutput>',
                   publicKeyId: '<cfoutput>#variables.amazonProperties.publicKeyID#</cfoutput>',
                      ledgerCurrency: 'USD',         
                      // customize the buyer experience
                      checkoutLanguage: 'en_US',
                      productType: 'PayAndShip',
                      placement: 'Cart',
                      buttonColor: 'Gold',
                      createCheckoutSessionConfig: {                     
                          <cfoutput>payloadJSON:'#variables.amazonProperties.buttonPayload#',
                          signature:'#getButtonSignature()#'</cfoutput>
                      }   
                  });
             </script>
        </cfsavecontent>
        <cfreturn buttonCode>
    </cffunction>

And this is where it is failing. When using the Amazon ScratchPad , all it needs is the checkoutSessionId, so I am completely lost at this point.

<cffunction name="getCheckoutSess" access="public" returntype="any">
    <cfargument name="sessionID" type="string" required="true">
    <cfreturn variables.oClient.getCheckoutSession(checkoutSessionId=arguments.sessionID)>
</cffunction>

The method is there in the WebStoreClient

/**
     * The GetCheckoutSession operation is used to get checkout session details that contain
     * all session associated details.
     *
     * @param checkoutSessionId Checkout Session ID provided by Checkout v2 service
     * @param header Map&lt;String, String&gt; containining key-value pair of required headers (e.g., keys such as x-amz-pay-idempotency-key, x-amz-pay-authtoken)
     * @return The response from the GetCheckoutSession service API, as
     * returned by Amazon Pay.
     * @throws AmazonPayClientException When an error response is returned by Amazon Pay due to bad request or other issue
     */
    public AmazonPayResponse getCheckoutSession(final String checkoutSessionId, final Map<String, String> header) throws AmazonPayClientException {
        final URI checkoutSessionURI = Util.getServiceURI(payConfiguration, ServiceConstants.CHECKOUT_SESSIONS);
        final URI getCheckoutSessionURI = checkoutSessionURI.resolve(checkoutSessionURI.getPath()   "/"   checkoutSessionId);
        return callAPI(getCheckoutSessionURI, "GET", null, "", header);
    }

    /**
     * The GetCheckoutSession operation is used to get checkout session details that contain
     * all session associated details.
     *
     * @param checkoutSessionId Checkout Session ID provided by Checkout v2 service
     * @return The response from the GetCheckoutSession service API, as
     * returned by Amazon Pay.
     * @throws AmazonPayClientException When an error response is returned by Amazon Pay due to bad request or other issue
     */
    public AmazonPayResponse getCheckoutSession(final String checkoutSessionId) throws AmazonPayClientException {
        return getCheckoutSession(checkoutSessionId, null);
    }

I am really hoping that someone, after reading all this, can say, oh, that's simple, you don't understand something basic..

CodePudding user response:

What version of ColdFusion are you using? Are you using javacast? If a function/method requires an INT or other specific datatype, you'll need to ensure that you are explicitly casting or java will throw that same error since the datatypes don't match up. (NOTE: Some datatypes may be non-standard CFML types. I've started casting values returned form a query because sometimes a CFML-string isn't a java-string.)

  • Related