Creating Button Forms Dynamically

Topics

The Amazon Simple Pay Getting Started Guide describes how to create Amazon Simple Pay buttons using the online forms Amazon has prepared for you. This works well when you only have a few buttons to create. If you have many buttons, you can create them programmatically.

Creating a Single Button Dynamically

The following process outlines creating a single button dynamically.

Creating a Single Button Form Dynamically

1Create the HTML for type of button you want to build. An example structure of each button is available in Amazon Simple Pay Button Input Reference.
2Set the product-specific parameters required by the HTML form. A button-specific parameter reference is also available in Amazon Simple Pay Button Input Reference.
3Set the values of the accessKey and secretKey parameters with your security credentials. For more information, see Using Access Identifiers and How to Add Your AWS Access Key to the Form.
4Create a string to sign using the form parameters. The parameters and values must be arranged in particular order. See How to Generate a Signature for the proper way to form the URL string.
5Calculate the signature and set it as the value for the signature parameter. For more information on generating a signature, see How to Generate a Signature.
6Generate the final button HTML, and add it to your web page.
7Repeat steps 1-6 for each button you plan to use.


[Important]Important

The preceding process includes steps 4-6 for signing the button because button parameters are sent in clear text and subject to tampering. You are not required to sign your button, but when you do, you are protecting the integrity of the data.

If you choose to not sign your parameters, you should verify the accuracy of the parameters manually in the Amazon Payments web site or the Instant Payment Notification data.

Amazon has published a set of samples in C#, Java, Perl and PHP that demonstrates how to do this in detail for each button type. For information on downloading the code, see the Amazon Flexible Payments Service Getting Started Guide.

Multiple Buttons Dynamically

If you have a larger product line, you application probably makes use of a dynamic catalog with a backend database for product information and transaction records. If so, you will want to use the referenceId parameter as a key to the transaction. This changes the process you use to support a large number of items.

While you can prepopulate button forms with many of the parameter values, some of them may only be determined at runtime. This is true not only of referenceId, but amount, subscriptionPeriod, and others which are set by the customer. Because you cannot calculate the signature until all the parameter values are known, for large applications the signature parameter must be done at runtime as well.

The following three-part process outlines one way to create multiple button forms dynamically.

First, build as much of the button as you can:

Preload your button html in your product database

1Create the HTML for each button instance you want. An example structure of each button is available in Amazon Simple Pay Button Input Reference. Instead of the payments.amazon.com (or payments-sandbox.amazon.com) endpoint, specify your own client-side page or asynchronous script. This script's role is described below.
2Set the known product-specific parameters required by each HTML form. A button-specific parameter reference is also available in Amazon Simple Pay Button Input Reference.
3Set the values of the accessKey and secretKey parameters using your security credentials. Also, set the signatureMethod and signatureVersion parameters with the values which represent your signing algorithm.
4Store each button html in a database field, and key it to the product it is configured to sell.
5Using the HTML created in step 4, create a string to sign using the form parameters. The parameters and values must be arranged in particular order. Include all parameters, even those for which you don't have values yet. See How to Generate a Signature for the proper way to form the URL string.
6Store the string to sign you created in step 5 in a database field, and key it to the product it is configured to sell.


Then, at runtime, you display the products to the user and collect any runtime input:

Collect runtime input

1Populate your catalog page with your incomplete buttons.
2Collect any information required from the user, setting appropriate button parameters.
3When the user submits the form, generate the unique value for the referenceId.
 The script you specified as the form action does the rest.


Finally, your custom client-side page or asynchronous script finishes the button and submits it:

Complete the transaction using your script

1Retrieve the string to sign for the product purchased from the database, and insert the runtime values, including the referenceId. Now you are ready to sign it.
2Calculate the signature and set it as the value for the signature parameter. For more information on generating a signature, see How to Generate a Signature
3Retrieve the HTML for the product purchased, and insert the runtime values, including the referenceId. Also insert the signature, and set the payments.amazon.com (or payments-sandbox.amazon.com) endpoint as the form action.
4Store the HTML in your database, and key it to the referenceId.
3Without displaying it again, submit the button. The user begins the Amazon payments pipeline.