Xbase++ Requires Chilkat v11.0.0+
Xbase++
Amazon Pay - Create Checkout Session
See more Amazon Pay Examples
Create a new Amazon Pay Checkout Session.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL i
LOCAL oPrivKey
LOCAL cPublicKeyId
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL nNullVal
LOCAL cConstraintId
LOCAL cDescription
LOCAL cStrVal
LOCAL cCheckoutSessionId
LOCAL cWebCheckoutDetailsCheckoutReviewReturnUrl
LOCAL cWebCheckoutDetailsCheckoutResultReturnUrl
LOCAL cWebCheckoutDetailsCheckoutCancelUrl
LOCAL cWebCheckoutDetailsAmazonPayRedirectUrl
LOCAL cProductType
LOCAL cChargePermissionType
LOCAL cRecurringMetadataFrequencyUnit
LOCAL cRecurringMetadataFrequencyValue
LOCAL cRecurringMetadataAmountAmount
LOCAL cRecurringMetadataAmountCurrencyCode
LOCAL cPaymentDetailsPaymentIntent
LOCAL nPaymentDetailsCanHandlePendingAuthorization
LOCAL cPaymentDetailsChargeAmount
LOCAL cPaymentDetailsTotalOrderAmount
LOCAL cPaymentDetailsSoftDescriptor
LOCAL cPaymentDetailsPresentmentCurrency
LOCAL cPaymentDetailsAllowOvercharge
LOCAL cPaymentDetailsExtendExpiration
LOCAL cMerchantMetadataMerchantReferenceId
LOCAL cMerchantMetadataMerchantStoreName
LOCAL cMerchantMetadataNoteToBuyer
LOCAL cMerchantMetadataCustomInformation
LOCAL cSupplementaryData
LOCAL cBuyer
LOCAL cBillingAddress
LOCAL cStatusDetailsState
LOCAL cStatusDetailsReasonCode
LOCAL cStatusDetailsReasonDescription
LOCAL cStatusDetailsLastUpdatedTimestamp
LOCAL cShippingAddress
LOCAL cPlatformId
LOCAL cChargePermissionId
LOCAL cChargeId
LOCAL cCreationTimestamp
LOCAL cExpirationTimestamp
LOCAL cStoreId
LOCAL cDeliverySpecificationsAddressRestrictionsType
LOCAL cProviderMetadataProviderReferenceId
LOCAL cReleaseEnvironment
LOCAL nCount_i
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
// 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": {}
// }
// }
// }
// }
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("webCheckoutDetails.checkoutReviewReturnUrl", "https://a.com/merchant-review-page")
// Make sure to use your storeId here:
oJson:UpdateString("storeId", "amzn1.application-oa2-client.8b5e45312b5248b69eeaStoreId")
i := 0
oJson:I := i
oJson:UpdateString("scopes[i]", "name")
i := i + 1
oJson:I := i
oJson:UpdateString("scopes[i]", "email")
i := i + 1
oJson:I := i
oJson:UpdateString("scopes[i]", "phoneNumber")
i := i + 1
oJson:I := i
oJson:UpdateString("scopes[i]", "billingAddress")
oJson:UpdateString("deliverySpecifications.specialRestrictions[0]", "RestrictPOBoxes")
oJson:UpdateString("deliverySpecifications.addressRestrictions.type", "Allowed")
oJson:UpdateString("deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions[0]", "WA")
oJson:UpdateString("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[0]", "95050")
oJson:UpdateString("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[1]", "93405")
oJson:UpdateString("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[0]", "72046")
oJson:UpdateString("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[1]", "72047")
oJson:UpdateString("deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions[0]", "AP")
oJson: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.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadPemFile("C:/someDir/myAmazonPayPrivateKey.pem")
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oHttp:destroy()
oJson:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// 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
cPublicKeyId := "SANDBOX-AHEGSJCM3L2S637RBGABLAFW"
nSuccess := oHttp:SetAuthPrivateKey(cPublicKeyId, oPrivKey)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oJson:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// 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
oHttp: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
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://pay-api.amazon.eu/sandbox/v2/checkoutSessions/", oJson, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oJson:destroy()
oPrivKey:destroy()
oResp:destroy()
RETURN
ENDIF
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0
// If the status code is not equal to 201, this will display error information.
? "Response Body:"
? oJResp:Emit()
nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode != 201)
// 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."
// }
? "Failed."
oHttp:destroy()
oJson:destroy()
oPrivKey:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
RETURN
ENDIF
// 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
cCheckoutSessionId := oJResp:StringOf("checkoutSessionId")
cWebCheckoutDetailsCheckoutReviewReturnUrl := oJResp:StringOf("webCheckoutDetails.checkoutReviewReturnUrl")
cWebCheckoutDetailsCheckoutResultReturnUrl := oJResp:StringOf("webCheckoutDetails.checkoutResultReturnUrl")
cWebCheckoutDetailsCheckoutCancelUrl := oJResp:StringOf("webCheckoutDetails.checkoutCancelUrl")
cWebCheckoutDetailsAmazonPayRedirectUrl := oJResp:StringOf("webCheckoutDetails.amazonPayRedirectUrl")
cProductType := oJResp:StringOf("productType")
cChargePermissionType := oJResp:StringOf("chargePermissionType")
cRecurringMetadataFrequencyUnit := oJResp:StringOf("recurringMetadata.frequency.unit")
cRecurringMetadataFrequencyValue := oJResp:StringOf("recurringMetadata.frequency.value")
cRecurringMetadataAmountAmount := oJResp:StringOf("recurringMetadata.amount.amount")
cRecurringMetadataAmountCurrencyCode := oJResp:StringOf("recurringMetadata.amount.currencyCode")
cPaymentDetailsPaymentIntent := oJResp:StringOf("paymentDetails.paymentIntent")
nPaymentDetailsCanHandlePendingAuthorization := oJResp:BoolOf("paymentDetails.canHandlePendingAuthorization")
cPaymentDetailsChargeAmount := oJResp:StringOf("paymentDetails.chargeAmount")
cPaymentDetailsTotalOrderAmount := oJResp:StringOf("paymentDetails.totalOrderAmount")
cPaymentDetailsSoftDescriptor := oJResp:StringOf("paymentDetails.softDescriptor")
cPaymentDetailsPresentmentCurrency := oJResp:StringOf("paymentDetails.presentmentCurrency")
cPaymentDetailsAllowOvercharge := oJResp:StringOf("paymentDetails.allowOvercharge")
cPaymentDetailsExtendExpiration := oJResp:StringOf("paymentDetails.extendExpiration")
cMerchantMetadataMerchantReferenceId := oJResp:StringOf("merchantMetadata.merchantReferenceId")
cMerchantMetadataMerchantStoreName := oJResp:StringOf("merchantMetadata.merchantStoreName")
cMerchantMetadataNoteToBuyer := oJResp:StringOf("merchantMetadata.noteToBuyer")
cMerchantMetadataCustomInformation := oJResp:StringOf("merchantMetadata.customInformation")
cSupplementaryData := oJResp:StringOf("supplementaryData")
cBuyer := oJResp:StringOf("buyer")
cBillingAddress := oJResp:StringOf("billingAddress")
cStatusDetailsState := oJResp:StringOf("statusDetails.state")
cStatusDetailsReasonCode := oJResp:StringOf("statusDetails.reasonCode")
cStatusDetailsReasonDescription := oJResp:StringOf("statusDetails.reasonDescription")
cStatusDetailsLastUpdatedTimestamp := oJResp:StringOf("statusDetails.lastUpdatedTimestamp")
cShippingAddress := oJResp:StringOf("shippingAddress")
cPlatformId := oJResp:StringOf("platformId")
cChargePermissionId := oJResp:StringOf("chargePermissionId")
cChargeId := oJResp:StringOf("chargeId")
cCreationTimestamp := oJResp:StringOf("creationTimestamp")
cExpirationTimestamp := oJResp:StringOf("expirationTimestamp")
cStoreId := oJResp:StringOf("storeId")
cDeliverySpecificationsAddressRestrictionsType := oJResp:StringOf("deliverySpecifications.addressRestrictions.type")
cProviderMetadataProviderReferenceId := oJResp:StringOf("providerMetadata.providerReferenceId")
cReleaseEnvironment := oJResp:StringOf("releaseEnvironment")
i := 0
nCount_i := oJResp:SizeOfArray("paymentPreferences")
DO WHILE i < nCount_i
oJResp:I := i
nNullVal := oJResp:IsNullOf("paymentPreferences[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("constraints")
DO WHILE i < nCount_i
oJResp:I := i
cConstraintId := oJResp:StringOf("constraints[i].constraintId")
cDescription := oJResp:StringOf("constraints[i].description")
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("deliverySpecifications.specialRestrictions")
DO WHILE i < nCount_i
oJResp:I := i
cStrVal := oJResp:StringOf("deliverySpecifications.specialRestrictions[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions")
DO WHILE i < nCount_i
oJResp:I := i
cStrVal := oJResp:StringOf("deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes")
DO WHILE i < nCount_i
oJResp:I := i
cStrVal := oJResp:StringOf("deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes")
DO WHILE i < nCount_i
oJResp:I := i
cStrVal := oJResp:StringOf("deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[i]")
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions")
DO WHILE i < nCount_i
oJResp:I := i
cStrVal := oJResp:StringOf("deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions[i]")
i := i + 1
ENDDO
oHttp:destroy()
oJson:destroy()
oPrivKey:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()