Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.89+

Amazon Pay - Update Charge Permission

See more Amazon Pay Examples

Update the Charge Permission with your external order metadata or the recurringMetadata.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

-- Implements the following CURL command:

-- curl "https://pay-api.amazon.com/:version/chargePermissions/:chargePermissionId" \
-- -X PATCH
-- -H "authorization:Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE"
-- -H "x-amz-pay-date:20201012T235046Z"
-- -d '{
--     "merchantMetadata": {
--         "merchantReferenceId": "32-41-323141-32",
--         "merchantStoreName": "AmazonTestStoreFront",
--         "noteToBuyer": "Some Note to buyer",
--         "customInformation": ""
--      }
-- }'

-- 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.

-- {
--     "merchantMetadata": {
--         "merchantReferenceId": "32-41-323141-32",
--         "merchantStoreName": "AmazonTestStoreFront",
--         "noteToBuyer": "Some Note to buyer",
--         "customInformation": ""
--      }
-- }

local json = chilkat.JsonObject.new()
json:update_string("merchantMetadata.merchantReferenceId", "32-41-323141-32")
json:update_string("merchantMetadata.merchantStoreName", "AmazonTestStoreFront")
json:update_string("merchantMetadata.noteToBuyer", "Some Note to buyer")
json:update_string("merchantMetadata.customInformation", "")

local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)

-- 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.
local privKey = chilkat.PrivateKey.new()
local success = privKey:load_pem_file("C:/someDir/myAmazonPayPrivateKey.pem")
if not success then
    print(privKey.last_error_text)
    return
end

-- 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.
local publicKeyId = "SANDBOX-AHEGSJCM3L2S637RBGABLAFW"
success = http:set_auth_private_key(publicKeyId, privKey)
if not success then
    print(http.last_error_text)
    return
end

-- 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
-- Authorization

http.accept = "application/json"

success = http:set_url_var("chargePermissionId", "P21-1111111-1111111")
-- To use the live system, replace "sandbox" with "live" in the URL passed to HttpSb.
-- Also, make sure to use the correct region: pay-api.amazon.com, pay-api.amazon.eu, or pay-api.amazon.jp
local resp = chilkat.HttpResponse.new()
success = http:http_sb("PATCH", "https://pay-api.amazon.eu/sandbox/v2/chargePermissions/{$chargePermissionId}", sbRequestBody, "utf-8", "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

-- If the status code is not equal to 200, this will display error information.
print("Response Body:")
print(jResp:emit())

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode ~= 200 then
    print("Failed.")
    return
end

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

-- {
--     "chargePermissionId": "chargePermission-1",
--     "chargePermissionReferenceId": null,
--     "buyer": {
--         "buyerId": "buyerId",
--         "name": "name-1",
--         "email": "name@amazon.com",
--         "phoneNumber": "800-000-0000",
--         "primeMembershipTypes": null
--     },
--     "releaseEnvironment": "Live",
--     "shippingAddress": {  // Null for PayOnly product type
--         "name": "Work",
--         "addressLine1": "440 Terry Ave",
--         "addressLine2": "",
--         "addressLine3": "",
--         "city": "Seattle",
--         "county": "King",
--         "district": "Seattle",
--         "stateOrRegion": "WA",
--         "postalCode": "98121",
--         "countryCode": "US",
--         "phoneNumber": "800-000-0000"
--     },
--     "billingAddress": {  // Only available in EU or for PayOnly product type
--         "name": "Work",
--         "addressLine1": "440 Terry Ave",
--         "addressLine2": "",
--         "addressLine3": "",
--         "city": "Seattle",
--         "county": "King",
--         "district": "Seattle",
--         "stateOrRegion": "WA",
--         "postalCode": "98121",
--         "countryCode": "US",
--         "phoneNumber": "800-000-0000"
--     },
--     "paymentPreferences": [
--     {
--         "paymentDescriptor": null
--     }],
--     "statusDetails": {
--         "state": "Chargeable",
--         "reasons": null,
--         "lastUpdatedTimestamp": "20190714T155300Z"
--     },
--     "creationTimestamp": "20190714T155300Z",
--     "expirationTimestamp": "20190715T155300Z",
--     "merchantMetadata": {
--         "merchantReferenceId": "123-77-876",
--         "merchantStoreName": "AmazonTestStoreFront",
--         "noteToBuyer": "merchantNoteForBuyer",
--         "customInformation": "This is custom information"
--     },
--     "platformId": "SPId",
--     "limits": {
--         "amountLimit": {
--             "amount": "14.00",
--             "currencyCode": "USD"
--         },
--         "amountBalance": {
--             "amount": "14.00",
--             "currencyCode": "USD"
--         }
--     },
--     "presentmentCurrency": "USD"
-- }

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

local paymentDescriptor = ""

local chargePermissionId = jResp:string_of("chargePermissionId")
local chargePermissionReferenceId = jResp:string_of("chargePermissionReferenceId")
local buyerBuyerId = jResp:string_of("buyer.buyerId")
local buyerName = jResp:string_of("buyer.name")
local buyerEmail = jResp:string_of("buyer.email")
local buyerPhoneNumber = jResp:string_of("buyer.phoneNumber")
local buyerPrimeMembershipTypes = jResp:string_of("buyer.primeMembershipTypes")
local releaseEnvironment = jResp:string_of("releaseEnvironment")
local shippingAddressName = jResp:string_of("shippingAddress.name")
local shippingAddressAddressLine1 = jResp:string_of("shippingAddress.addressLine1")
local shippingAddressAddressLine2 = jResp:string_of("shippingAddress.addressLine2")
local shippingAddressAddressLine3 = jResp:string_of("shippingAddress.addressLine3")
local shippingAddressCity = jResp:string_of("shippingAddress.city")
local shippingAddressCounty = jResp:string_of("shippingAddress.county")
local shippingAddressDistrict = jResp:string_of("shippingAddress.district")
local shippingAddressStateOrRegion = jResp:string_of("shippingAddress.stateOrRegion")
local shippingAddressPostalCode = jResp:string_of("shippingAddress.postalCode")
local shippingAddressCountryCode = jResp:string_of("shippingAddress.countryCode")
local shippingAddressPhoneNumber = jResp:string_of("shippingAddress.phoneNumber")
local billingAddressName = jResp:string_of("billingAddress.name")
local billingAddressAddressLine1 = jResp:string_of("billingAddress.addressLine1")
local billingAddressAddressLine2 = jResp:string_of("billingAddress.addressLine2")
local billingAddressAddressLine3 = jResp:string_of("billingAddress.addressLine3")
local billingAddressCity = jResp:string_of("billingAddress.city")
local billingAddressCounty = jResp:string_of("billingAddress.county")
local billingAddressDistrict = jResp:string_of("billingAddress.district")
local billingAddressStateOrRegion = jResp:string_of("billingAddress.stateOrRegion")
local billingAddressPostalCode = jResp:string_of("billingAddress.postalCode")
local billingAddressCountryCode = jResp:string_of("billingAddress.countryCode")
local billingAddressPhoneNumber = jResp:string_of("billingAddress.phoneNumber")
local statusDetailsState = jResp:string_of("statusDetails.state")
local statusDetailsReasons = jResp:string_of("statusDetails.reasons")
local statusDetailsLastUpdatedTimestamp = jResp:string_of("statusDetails.lastUpdatedTimestamp")
local creationTimestamp = jResp:string_of("creationTimestamp")
local expirationTimestamp = jResp:string_of("expirationTimestamp")
local merchantMetadataMerchantReferenceId = jResp:string_of("merchantMetadata.merchantReferenceId")
local merchantMetadataMerchantStoreName = jResp:string_of("merchantMetadata.merchantStoreName")
local merchantMetadataNoteToBuyer = jResp:string_of("merchantMetadata.noteToBuyer")
local merchantMetadataCustomInformation = jResp:string_of("merchantMetadata.customInformation")
local platformId = jResp:string_of("platformId")
local limitsAmountLimitAmount = jResp:string_of("limits.amountLimit.amount")
local limitsAmountLimitCurrencyCode = jResp:string_of("limits.amountLimit.currencyCode")
local limitsAmountBalanceAmount = jResp:string_of("limits.amountBalance.amount")
local limitsAmountBalanceCurrencyCode = jResp:string_of("limits.amountBalance.currencyCode")
local presentmentCurrency = jResp:string_of("presentmentCurrency")
local i = 0
local count_i = jResp:size_of_array("paymentPreferences")
while i < count_i do
    jResp.i = i
    paymentDescriptor = jResp:string_of("paymentPreferences[i].paymentDescriptor")
    i = i + 1
end