Code Samples

Topics

This appendix provides an overview of the Amazon Flexible Payments Service development libraries provided by Amazon. The sample code shows you how to implement most of the basic Amazon FPS functions. Packaged in four programming languages (C#, Java, Perl, and PHP), the development libraries are available from the Amazon Web Services developer community, under the Amazon Flexible Payments Service category. Refer to the following table for specific sample packages.

Language

Location

C#

http://signaturev2-fps.s3.amazonaws.com/ amazon-fps-2008-09-17-cs-library.zip

Java

http://signaturev2-fps.s3.amazonaws.com/ amazon-fps-2008-09-17-java-library.zip

Perl

http://signaturev2-fps.s3.amazonaws.com/ amazon-fps-2008-09-17-perl-library.zip

PHP

http://signaturev2-fps.s3.amazonaws.com/ amazon-fps-2008-09-17-php5-library.zip

Each package is updated for signature version 2, and contains both a development library and a collection of sample implementations of the Amazon FPS APIs. The development libraries enable you to

The following sections describe the code libraries in more detail, with an eye towards enabling you to build your applications quickly. If you want help building your first sample application using the development libraries, see "Making a Pay Request" in the Amazon Flexible Payments Service Getting Started Guide

Understanding the Amazon CBUI Samples

Amazon provides five samples in four programming languages (C#, Java, Perl, and PHP) which show you how to build Co-Branded User Interface request URLs.

When you download a sample file, such as amazon-fps-2008-09-17-java-library, the [package root]/src/com/amazonaws/cbui/samples folder contains sample classes showing how to generate pipeline-specific CBUI URLs from your code ([package root] is the location you extracted your sample package).

Each sample describes its requirements in its Readme.html file, located at the package root. Typically, the entire library structure must be available to the compiler. For example, the amazon-fps-2008-09-17-php-library.zip file contains the src/Amazon/CBUI and folders which the files in src/Amazon/CBUI/Samples require.

In addition to these primary components, a sample may include other required resources. For example, the Java samples all include numerous jar files in the [package-root]/third-party folder, which must also be in your classpath in order to compile the sample.

For each sample, you must set your security credentials and Amazon FPS sandbox endpoints in a library-dependant way. For example, to use the C# library, you set your security credentials in the [package-root]/src/Amazon.FPS.Samples/Amazon.FPS.Samples/AmazonFPSSamples.cs file, while for the perl library you set them in the individual [package-root]/src/Amazon/CBUI/Samples/*.pl file you are working with.

The following samples are provided with each sample library:

Class

Description

CBUIEditTokenPipeline Sample

Requests authorization for a one-time payment.

CBUIMulitUsePipeline Sample

Requests authorization for multiple payments.

CBUIRecipientPipeline Sample

Requests authorization for a recipient token pipeline, such as that needed for marketplace fixed and variable fees.

CBUIRecurringTokenPipeline Sample

Requests authorization for a recurring token pipeline, such as that needed for periodic charges.

CBUISingleUsePipelineSample

Requests authorization for an edit-token pipeline.

In the following section, we show how to work with the CBUISingleUsePipeline sample using the Java library. This sample enables you to set up a single-use token for a one-time payment. You will find that the basic process you use for the CBUISingleUsePipeline sample is the same for all the other samples in the CBUI/Samples (or, in the case of Amazon.CBUI.Samples) folder. (The process for the FPS and Return URL/IPN Validations samples are different. For more information, see Understanding the Amazon FPS Samples.)

Java

This section describes the Java version of the CBUISingleUsePipeline. The files for the C#, Perl, and PHP CBUISingleUsePipeline samples are listed in Locations of the CBUISingleUsePipeline Files in Other Libraries

The CBUISingleUsePipeline sample centers on the following files:

File

Description

config.properties

Set your AWS access key ID, AWS secret key, and sandbox endpoint in this file.

CBUISingleUsePipelineSample.javaIn the main method, you create an AmazonFPSSingleUsePipeline object and use it to add parameters specific to your application.
AmazonFPSSingleUsePipeline.javaInvoked from CBUISingleUsePipelineSample.java, this class contains the setMandatoryParameters and validateParameters functions which you can customize for your application.

Co-Branded service request with Java SDK Sample

1

Open the file [package-root]/src/config.properties, and set AwsAccessKey and AwsSecretKey properties to your AWS access key and AWS secret key, respectively. To get your security credentials, see Getting an AWS Account in the Amazon Flexible Payments Service Getting Started Guide.

2

In the same file, set the AwsServiceEndPoint to https://fps.sandbox.amazonaws.com/ (the Amazon FPS sandbox).

3

In the same file, set the CBUIServiceEndPoint to https://authorize.payments-sandbox.amazon.com/cobranded-ui/actions/start (the Co-Branded service sandbox).

4

Open the file [package-root]/src/com/amazonaws/cbui/ samples/CBUISingleUsePipelineSample.java, and find the following line:

AmazonFPSSingleUsePipeline pipeline= new AmazonFPSSingleUsePipeline(accessKey, secretKey);

Change the pipeline.setMandatoryParameters and pipeline.addParameters method calls to the following:

//pipeline name, your return URL, and the amount
pipeline.setMandatoryParameters("callerReferenceSingleUse", 
"[your returnUrl]", "5"); 

//optional parameters
pipeline.addParameter("currencyCode", "USD");
pipeline.addParameter("paymentReason", "Now and Forever - Richard Mark");
pipeline.addParameter("paymentMethod", "ABT,ACH,CC">;
pipeline.addParameter("callerReference", "[Unique ID for the transaction]");

Save the file.

5

Ensure that all the jar files in the third-party folder and sub folders are in your java CLASSPATH.

6

Compile and run the sample. The Co-branded authorization page is printed to standard out.

7

Using a web browser, navigate to the URL produced by the sample. You must use an account different from your AWS developer or business accounts.

8

When complete, the page you specified as [your returnUrl] is hit with the Co-Branded service response. If signed, you validate this response by testing the signature..


You can customize the sample by modifying the file [package-root]/src/com/amazonaws/cbui/AmazonFPSSingleUsePipeline.java. The setMandatoryParameters only requires callerReference, returnUrl, and transactionAmount. If you want to make more parameters mandatory, modify this method.

In the same file, the validateParameters function ensures that the transactionAmount parameter is present. You can add custom validation checks to this method.

Locations of the CBUISingleUsePipeline Files in Other Libraries

The development libraries for C#, Perl, and PHP also enable you to create CBUI pipeline urls. The following tables indicate the locations of the files referenced in Understanding the Amazon CBUI Samples.

C# File Locations for the Amazon.FPS CBUI Sample

File

Location

CBUISingleUsePipelineSample.cs

[package root]\src\Amazon.CBUI\ Amazon.CBUI.Model.

AmazonFPSSingleUsePipeline.cs[package root]\src\Amazon.CBUI.Samples\ Amazon.CBUI.Samples
Amazon.CBUI.proj

(Visual Studio.NET solution for the development library)

[package root]\src\Amazon.CBUI\ Amazon.CBUI\

Amazon.CBUI.Samples.proj

(Visual Studio.NET solution for the Amazon FPS API samples)

[package root]\src\Amazon.CBUI\Amazon.CBUI.Samples\ Amazon.CBUI.Samples
Amazon.FPS.sln

Visual Studio.NET solution for the library package

[Note]Note

The Visual Studio.NET samples are organized into this solution. After setting your access parameters the first time, you build the entire solution to generate the dependency classes. Then you modify the specific sample you want. See the Readme.html file for more information.

[package root]/src/Amazon.FPS/Amazon.FPS

Perl File Locations for the Amazon.FPS VerifySignature Sample

Class

Location

CBUISingleUsePipelineSample.pl

[package root]/src/Amazon/CBUI/Samples.

AmazonFPSSingleUsePipeline.pm[package root]/src/Amazon/CBUI
ReadMe.html (readme for perl fps library)[package root]/src

PHP File Locations for the Amazon.FPS VerifySignature Sample

Class

Location

CBUISingleUsePipelineSample.php

[package root]/src/Amazon/CBUI/Model.

CBUISingleUsePipeline.php[package root]/src/Amazon/CBUI/Samples
ReadMe.html (readme for php fps library)[package root]/src