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.
The following process outlines creating a single button dynamically.
Creating a Single Button Form Dynamically
| 1 | Create 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. |
| 2 | Set the product-specific parameters required by the HTML form. A button-specific parameter reference is also available in Amazon Simple Pay Button Input Reference. |
| 3 | Set 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. |
| 4 | Create 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. |
| 5 | Calculate 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. |
| 6 | Generate the final button HTML, and add it to your web page. |
| 7 | Repeat steps 1-6 for each button you plan to use. |
![]() | 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.
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
| 1 | Create 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. |
| 2 | Set 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. |
| 3 | Set 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. |
| 4 | Store each button html in a database field, and key it to the product it is configured to sell. |
| 5 | Using 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. |
| 6 | Store 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
| 1 | Populate your catalog page with your incomplete buttons. |
| 2 | Collect any information required from the user, setting appropriate button parameters. |
| 3 | When 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
| 1 | Retrieve 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. |
| 2 | Calculate 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 |
| 3 | Retrieve 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. |
| 4 | Store the HTML in your database, and key it to the referenceId. |
| 3 | Without displaying it again, submit the button. The user begins the Amazon payments pipeline. |