Sample code for 30+ languages & platforms
PowerBuilder

Amazon Pay - Complete Checkout Session

See more Amazon Pay Examples

Complete Checkout Session after the buyer returns to checkoutResultReturnUrl to finalize the paymentIntent.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Json
oleobject loo_PrivKey
string ls_PublicKeyId
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
integer li_NullVal
string ls_CheckoutSessionId
string ls_WebCheckoutDetails
string ls_ChargePermissionType
string ls_RecurringMetadata
string ls_ProductType
string ls_PaymentDetails
string ls_MerchantMetadata
string ls_SupplementaryData
string ls_Buyer
string ls_BillingAddress
string ls_StatusDetailsState
string ls_StatusDetailsReasonCode
string ls_StatusDetailsReasonDescription
string ls_StatusDetailsLastUpdatedTimestamp
string ls_ShippingAddress
string ls_PlatformId
string ls_ChargePermissionId
string ls_ChargeId
string ls_CreationTimestamp
string ls_ExpirationTimestamp
string ls_StoreId
string ls_DeliverySpecifications
string ls_ProviderMetadata
string ls_ReleaseEnvironment
integer i
integer li_Count_i

li_Success = 0

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Implements the following CURL command:

// curl "https://pay-api.amazon.com/:version/checkoutSessions/:checkoutSessionId/complete" \
// -X POST
// -H "authorization:Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE"
// -H "x-amz-pay-date:20201012T235046Z"
// -H "x-amz-pay-idempotency-key:AVLo5tI10BHgEk2jEXAMPLEKEY"
// -d '{
//     "chargeAmount": {
//         "amount": "14.00",
//         "currencyCode": "USD"
//     }
// }'

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "chargeAmount": {
//     "amount": "14.00",
//     "currencyCode": "USD"
//   }
// }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("chargeAmount.amount","14.00")
loo_Json.UpdateString("chargeAmount.currencyCode","USD")

// Load your Amazon Pay private key.  
// There are many other ways to load private keys into the Chilkat private key object, such as from different formats,
// or from in-memory strings or bytes.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadPemFile("C:/someDir/myAmazonPayPrivateKey.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Http
    destroy loo_Json
    destroy loo_PrivKey
    return
end if

// Provide your Amazon Pay private key and Public Key ID 
// Use your public key ID here.  It must be the one associated with the private key.
// Note: The SetAuthPrivateKey method was added in Chilkat v9.5.0.89
ls_PublicKeyId = "SANDBOX-AHEGSJCM3L2S637RBGABLAFW"
li_Success = loo_Http.SetAuthPrivateKey(ls_PublicKeyId,loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    destroy loo_PrivKey
    return
end if

// Note: When the private key is provided as shown above, Chilkat will automatically add the required x-amz-pay-* headers to the HTTP request,
// and will also sign the request.  Nothing more is needed.
// Chilkat automatically generates and adds the following headers:
// 
// x-amz-pay-date
// x-amz-pay-host
// x-amz-pay-region
// x-amz-pay-idempotency-key
// Authorization

loo_Http.Accept = "application/json"

// To use the live system, replace "sandbox" with "live" in the URL passed to HttpJson.
// Also, make sure to use the correct region: pay-api.amazon.com, pay-api.amazon.eu, or pay-api.amazon.jp
li_Success = loo_Http.SetUrlVar("sessionId","62b7d028-6c7c-4a43-b077-3897dff27c5b")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpJson("POST","https://pay-api.amazon.eu/sandbox/v2/checkoutSessions/{$sessionId}/complete",loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    destroy loo_PrivKey
    destroy loo_Resp
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

// If the status code is not equal to 200, this will display error information.
Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode <> 200 then

    // A sample response body for the case where the session ID does not exist:
    // 	{
    // 	  "reasonCode": "ResourceNotFound",
    // 	  "message": "Resource you are trying to access is not available. Requested path '/sandbox/v2/checkoutSessions/bd504926-f659-4ad7-a1a9-9a747aaf5275'"
    // 	}

    // Another sample failure response (status code = 422)
    // 	{
    // 	  "reasonCode": "InvalidCheckoutSessionStatus",
    // 	  "message": "You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed"
    // 	}

    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Json
    destroy loo_PrivKey
    destroy loo_Resp
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "checkoutSessionId": "bd504926-f659-4ad7-a1a9-9a747aaf5275",
//   "webCheckoutDetails": null,
//   "chargePermissionType": "OneTime",
//   "recurringMetadata": null,
//   "productType": null,
//   "paymentDetails": null,
//   "merchantMetadata": null,
//   "supplementaryData": null,
//   "buyer": null,
//   "billingAddress": null,
//   "paymentPreferences": [
//     null
//   ],
//   "statusDetails": {
//     "state": "Completed",
//     "reasonCode": null,
//     "reasonDescription": null,
//     "lastUpdatedTimestamp": "20191015T204327Z"
//   },
//   "shippingAddress": null,
//   "platformId": null,
//   "chargePermissionId": "S01-5105180-3221187",
//   "chargeId": "S01-5105180-3221187-C056351",
//   "constraints": [
//     null
//   ],
//   "creationTimestamp": "20191015T204313Z",
//   "expirationTimestamp": null,
//   "storeId": null,
//   "deliverySpecifications": null,
//   "providerMetadata": null,
//   "releaseEnvironment": null
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

ls_CheckoutSessionId = loo_JResp.StringOf("checkoutSessionId")
ls_WebCheckoutDetails = loo_JResp.StringOf("webCheckoutDetails")
ls_ChargePermissionType = loo_JResp.StringOf("chargePermissionType")
ls_RecurringMetadata = loo_JResp.StringOf("recurringMetadata")
ls_ProductType = loo_JResp.StringOf("productType")
ls_PaymentDetails = loo_JResp.StringOf("paymentDetails")
ls_MerchantMetadata = loo_JResp.StringOf("merchantMetadata")
ls_SupplementaryData = loo_JResp.StringOf("supplementaryData")
ls_Buyer = loo_JResp.StringOf("buyer")
ls_BillingAddress = loo_JResp.StringOf("billingAddress")
ls_StatusDetailsState = loo_JResp.StringOf("statusDetails.state")
ls_StatusDetailsReasonCode = loo_JResp.StringOf("statusDetails.reasonCode")
ls_StatusDetailsReasonDescription = loo_JResp.StringOf("statusDetails.reasonDescription")
ls_StatusDetailsLastUpdatedTimestamp = loo_JResp.StringOf("statusDetails.lastUpdatedTimestamp")
ls_ShippingAddress = loo_JResp.StringOf("shippingAddress")
ls_PlatformId = loo_JResp.StringOf("platformId")
ls_ChargePermissionId = loo_JResp.StringOf("chargePermissionId")
ls_ChargeId = loo_JResp.StringOf("chargeId")
ls_CreationTimestamp = loo_JResp.StringOf("creationTimestamp")
ls_ExpirationTimestamp = loo_JResp.StringOf("expirationTimestamp")
ls_StoreId = loo_JResp.StringOf("storeId")
ls_DeliverySpecifications = loo_JResp.StringOf("deliverySpecifications")
ls_ProviderMetadata = loo_JResp.StringOf("providerMetadata")
ls_ReleaseEnvironment = loo_JResp.StringOf("releaseEnvironment")
i = 0
li_Count_i = loo_JResp.SizeOfArray("paymentPreferences")
do while i < li_Count_i
    loo_JResp.I = i
    li_NullVal = loo_JResp.IsNullOf("paymentPreferences[i]")
    i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("constraints")
do while i < li_Count_i
    loo_JResp.I = i
    li_NullVal = loo_JResp.IsNullOf("constraints[i]")
    i = i + 1
loop


destroy loo_Http
destroy loo_Json
destroy loo_PrivKey
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp