PowerBuilder
PowerBuilder
Amazon Pay - Create Checkout Session
See more Amazon Pay Examples
Create a new Amazon Pay Checkout Session.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Json
integer i
oleobject loo_PrivKey
string ls_PublicKeyId
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
integer li_NullVal
string ls_ConstraintId
string ls_Description
string ls_StrVal
string ls_CheckoutSessionId
string ls_WebCheckoutDetailsCheckoutReviewReturnUrl
string ls_WebCheckoutDetailsCheckoutResultReturnUrl
string ls_WebCheckoutDetailsCheckoutCancelUrl
string ls_WebCheckoutDetailsAmazonPayRedirectUrl
string ls_ProductType
string ls_ChargePermissionType
string ls_RecurringMetadataFrequencyUnit
string ls_RecurringMetadataFrequencyValue
string ls_RecurringMetadataAmountAmount
string ls_RecurringMetadataAmountCurrencyCode
string ls_PaymentDetailsPaymentIntent
integer li_PaymentDetailsCanHandlePendingAuthorization
string ls_PaymentDetailsChargeAmount
string ls_PaymentDetailsTotalOrderAmount
string ls_PaymentDetailsSoftDescriptor
string ls_PaymentDetailsPresentmentCurrency
string ls_PaymentDetailsAllowOvercharge
string ls_PaymentDetailsExtendExpiration
string ls_MerchantMetadataMerchantReferenceId
string ls_MerchantMetadataMerchantStoreName
string ls_MerchantMetadataNoteToBuyer
string ls_MerchantMetadataCustomInformation
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_DeliverySpecificationsAddressRestrictionsType
string ls_ProviderMetadataProviderReferenceId
string ls_ReleaseEnvironment
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/" \
// -X POST
// -H "authorization:Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE"
// -H "x-amz-pay-date:20201012T235046Z"
// -H "x-amz-pay-idempotency-key:AVLo5tI10BHgEk2jEXAMPLEKEY"
// -d '{
// "webCheckoutDetails": {
// "checkoutReviewReturnUrl": "https://a.com/merchant-review-page"
// },
// "storeId": "amzn1.application-oa2-client.8b5e45312b5248b69eeaStoreId",
// "scopes": ["name", "email", "phoneNumber", "billingAddress"],
// "deliverySpecifications": {
// "specialRestrictions": ["RestrictPOBoxes"],
// "addressRestrictions": {
// "type": "Allowed",
// "restrictions": {
// "US": {
// "statesOrRegions": ["WA"],
// "zipCodes": ["95050", "93405"]
// },
// "GB": {
// "zipCodes": ["72046", "72047"]
// },
// "IN": {
// "statesOrRegions": ["AP"]
// },
// "JP": {}
// }
// }
// }
// }'
// 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.
// {
// "webCheckoutDetails": {
// "checkoutReviewReturnUrl": "https://a.com/merchant-review-page"
// },
// "storeId": "amzn1.application-oa2-client.8b5e45312b5248b69eeaStoreId",
// "scopes": [
// "name",
// "email",
// "phoneNumber",
// "billingAddress"
// ],
// "deliverySpecifications": {
// "specialRestrictions": [
// "RestrictPOBoxes"
// ],
// "addressRestrictions": {
// "type": "Allowed",
// "restrictions": {
// "US": {
// "statesOrRegions": [
// "WA"
// ],
// "zipCodes": [
// "95050",
// "93405"
// ]
// },
// "GB": {
// "zipCodes": [
// "72046",
// "72047"
// ]
// },
// "IN": {
// "statesOrRegions": [
// "AP"
// ]
// },
// "JP": {}
// }
// }
// }
// }
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("webCheckoutDetails.checkoutReviewReturnUrl","https://a.com/merchant-review-page")
// Make sure to use your storeId here:
loo_Json.UpdateString("storeId","amzn1.application-oa2-client.8b5e45312b5248b69eeaStoreId")
i = 0
loo_Json.I = i
loo_Json.UpdateString("scopes[i]","name")
i = i + 1
loo_Json.I = i
loo_Json.UpdateString("scopes[i]","email")
i = i + 1
loo_Json.I = i
loo_Json.UpdateString("scopes[i]","phoneNumber")
i = i + 1
loo_Json.I = i
loo_Json.UpdateString("scopes[i]","billingAddress")
loo_Json.UpdateString("deliverySpecifications.specialRestrictions[0]","RestrictPOBoxes")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.type","Allowed")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions[0]","WA")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[0]","95050")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[1]","93405")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[0]","72046")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[1]","72047")
loo_Json.UpdateString("deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions[0]","AP")
loo_Json.UpdateNewObject("deliverySpecifications.addressRestrictions.restrictions.JP")
// 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
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/",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 201, 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 <> 201 then
// The jResp would've contained an error message, similar to this:
// {
// "reasonCode": "InvalidHeaderValue",
// "message": "The value AF2EGWREIPHV2S2TUFFBRDGM provided for PublicKeyId is invalid. Please verify that you are using the PublicKeyId in the correct region and environment."
// }
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": {
// "checkoutReviewReturnUrl": "https://a.com/merchant-review-page",
// "checkoutResultReturnUrl": null,
// "checkoutCancelUrl": null,
// "amazonPayRedirectUrl": null
// },
// "productType": "PayAndShip",
// "chargePermissionType": "Recurring",
// "recurringMetadata": {
// "frequency": {
// "unit": "Month",
// "value": "1"
// },
// "amount": {
// "amount": "30",
// "currencyCode": "USD"
// }
// },
// "paymentDetails": {
// "paymentIntent": null,
// "canHandlePendingAuthorization": false,
// "chargeAmount": null,
// "totalOrderAmount": null,
// "softDescriptor": null,
// "presentmentCurrency": null,
// "allowOvercharge": null,
// "extendExpiration": null
// },
// "merchantMetadata": {
// "merchantReferenceId": null,
// "merchantStoreName": null,
// "noteToBuyer": null,
// "customInformation": null
// },
// "supplementaryData": null,
// "buyer": null,
// "billingAddress": null,
// "paymentPreferences": [
// null
// ],
// "statusDetails": {
// "state": "Open",
// "reasonCode": null,
// "reasonDescription": null,
// "lastUpdatedTimestamp": "20191015T204327Z"
// },
// "shippingAddress": null,
// "platformId": null,
// "chargePermissionId": null,
// "chargeId": null,
// "constraints": [
// {
// "constraintId": "BuyerNotAssociated",
// "description": "There is no buyer associated with the Checkout Session. Return the checkout session id to the Amazon Pay Button to allow buyer to login."
// },
// {
// "constraintId": "ChargeAmountNotSet",
// "description": "chargeAmount is not set."
// },
// {
// "constraintId": "CheckoutResultReturnUrlNotSet",
// "description": "checkoutResultReturnUrl is not set."
// },
// {
// "constraintId": "PaymentIntentNotSet",
// "description": "paymentIntent is not set."
// }
// ],
// "creationTimestamp": "20191015T204313Z",
// "expirationTimestamp": "20191016T204313Z",
// "storeId": "amzn1.application-oa2-client.8b5e45312b5248b69eeaStoreId",
// "deliverySpecifications": {
// "specialRestrictions": [
// "RestrictPOBoxes"
// ],
// "addressRestrictions": {
// "type": "Allowed",
// "restrictions": {
// "US": {
// "statesOrRegions": [
// "WA"
// ],
// "zipCodes": [
// "95050",
// "93405"
// ]
// },
// "GB": {
// "zipCodes": [
// "72046",
// "72047"
// ]
// },
// "IN": {
// "statesOrRegions": [
// "AP"
// ]
// },
// "JP": {}
// }
// }
// },
// "providerMetadata": {
// "providerReferenceId": null
// },
// "releaseEnvironment": "Sandbox"
// }
// 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_WebCheckoutDetailsCheckoutReviewReturnUrl = loo_JResp.StringOf("webCheckoutDetails.checkoutReviewReturnUrl")
ls_WebCheckoutDetailsCheckoutResultReturnUrl = loo_JResp.StringOf("webCheckoutDetails.checkoutResultReturnUrl")
ls_WebCheckoutDetailsCheckoutCancelUrl = loo_JResp.StringOf("webCheckoutDetails.checkoutCancelUrl")
ls_WebCheckoutDetailsAmazonPayRedirectUrl = loo_JResp.StringOf("webCheckoutDetails.amazonPayRedirectUrl")
ls_ProductType = loo_JResp.StringOf("productType")
ls_ChargePermissionType = loo_JResp.StringOf("chargePermissionType")
ls_RecurringMetadataFrequencyUnit = loo_JResp.StringOf("recurringMetadata.frequency.unit")
ls_RecurringMetadataFrequencyValue = loo_JResp.StringOf("recurringMetadata.frequency.value")
ls_RecurringMetadataAmountAmount = loo_JResp.StringOf("recurringMetadata.amount.amount")
ls_RecurringMetadataAmountCurrencyCode = loo_JResp.StringOf("recurringMetadata.amount.currencyCode")
ls_PaymentDetailsPaymentIntent = loo_JResp.StringOf("paymentDetails.paymentIntent")
li_PaymentDetailsCanHandlePendingAuthorization = loo_JResp.BoolOf("paymentDetails.canHandlePendingAuthorization")
ls_PaymentDetailsChargeAmount = loo_JResp.StringOf("paymentDetails.chargeAmount")
ls_PaymentDetailsTotalOrderAmount = loo_JResp.StringOf("paymentDetails.totalOrderAmount")
ls_PaymentDetailsSoftDescriptor = loo_JResp.StringOf("paymentDetails.softDescriptor")
ls_PaymentDetailsPresentmentCurrency = loo_JResp.StringOf("paymentDetails.presentmentCurrency")
ls_PaymentDetailsAllowOvercharge = loo_JResp.StringOf("paymentDetails.allowOvercharge")
ls_PaymentDetailsExtendExpiration = loo_JResp.StringOf("paymentDetails.extendExpiration")
ls_MerchantMetadataMerchantReferenceId = loo_JResp.StringOf("merchantMetadata.merchantReferenceId")
ls_MerchantMetadataMerchantStoreName = loo_JResp.StringOf("merchantMetadata.merchantStoreName")
ls_MerchantMetadataNoteToBuyer = loo_JResp.StringOf("merchantMetadata.noteToBuyer")
ls_MerchantMetadataCustomInformation = loo_JResp.StringOf("merchantMetadata.customInformation")
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_DeliverySpecificationsAddressRestrictionsType = loo_JResp.StringOf("deliverySpecifications.addressRestrictions.type")
ls_ProviderMetadataProviderReferenceId = loo_JResp.StringOf("providerMetadata.providerReferenceId")
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
ls_ConstraintId = loo_JResp.StringOf("constraints[i].constraintId")
ls_Description = loo_JResp.StringOf("constraints[i].description")
i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("deliverySpecifications.specialRestrictions")
do while i < li_Count_i
loo_JResp.I = i
ls_StrVal = loo_JResp.StringOf("deliverySpecifications.specialRestrictions[i]")
i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions")
do while i < li_Count_i
loo_JResp.I = i
ls_StrVal = loo_JResp.StringOf("deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions[i]")
i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes")
do while i < li_Count_i
loo_JResp.I = i
ls_StrVal = loo_JResp.StringOf("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[i]")
i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes")
do while i < li_Count_i
loo_JResp.I = i
ls_StrVal = loo_JResp.StringOf("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[i]")
i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions")
do while i < li_Count_i
loo_JResp.I = i
ls_StrVal = loo_JResp.StringOf("deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions[i]")
i = i + 1
loop
destroy loo_Http
destroy loo_Json
destroy loo_PrivKey
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp