Sample code for 30+ languages & platforms
Unicode C

Amazon Pay - Close Charge Permission

See more Amazon Pay Examples

Moves the Charge Permission to a Closed state. No future charges can be made and pending charges will be canceled if you set cancelPendingCharges to true.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkPrivateKeyW privKey;
    const wchar_t *publicKeyId;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *paymentDescriptor;
    const wchar_t *reasonCode;
    const wchar_t *reasonDescription;
    const wchar_t *chargePermissionId;
    const wchar_t *chargePermissionReferenceId;
    const wchar_t *buyerBuyerId;
    const wchar_t *buyerName;
    const wchar_t *buyerEmail;
    const wchar_t *buyerPhoneNumber;
    const wchar_t *buyerPrimeMembershipTypes;
    const wchar_t *releaseEnvironment;
    const wchar_t *shippingAddressName;
    const wchar_t *shippingAddressAddressLine1;
    const wchar_t *shippingAddressAddressLine2;
    const wchar_t *shippingAddressAddressLine3;
    const wchar_t *shippingAddressCity;
    const wchar_t *shippingAddressCounty;
    const wchar_t *shippingAddressDistrict;
    const wchar_t *shippingAddressStateOrRegion;
    const wchar_t *shippingAddressPostalCode;
    const wchar_t *shippingAddressCountryCode;
    const wchar_t *shippingAddressPhoneNumber;
    const wchar_t *billingAddressName;
    const wchar_t *billingAddressAddressLine1;
    const wchar_t *billingAddressAddressLine2;
    const wchar_t *billingAddressAddressLine3;
    const wchar_t *billingAddressCity;
    const wchar_t *billingAddressCounty;
    const wchar_t *billingAddressDistrict;
    const wchar_t *billingAddressStateOrRegion;
    const wchar_t *billingAddressPostalCode;
    const wchar_t *billingAddressCountryCode;
    const wchar_t *billingAddressPhoneNumber;
    const wchar_t *statusDetailsState;
    const wchar_t *statusDetailsLastUpdatedTimestamp;
    const wchar_t *creationTimestamp;
    const wchar_t *expirationTimestamp;
    const wchar_t *merchantMetadataMerchantReferenceId;
    const wchar_t *merchantMetadataMerchantStoreName;
    const wchar_t *merchantMetadataNoteToBuyer;
    const wchar_t *merchantMetadataCustomInformation;
    const wchar_t *platformId;
    const wchar_t *limitsAmountLimitAmount;
    const wchar_t *limitsAmountLimitCurrencyCode;
    const wchar_t *limitsAmountBalanceAmount;
    const wchar_t *limitsAmountBalanceCurrencyCode;
    const wchar_t *presentmentCurrency;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Implements the following CURL command:

    // curl "https://pay-api.amazon.com/:version/chargePermissions/:chargePermissionId/close" \
    // -X DELETE
    // -H "authorization:Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE"
    // -H "x-amz-pay-date:20201012T235046Z"
    // -d '{
    //     "closureReason": "No more charges required",
    //     "cancelPendingCharges": false
    // }'

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

    // {
    //     "closureReason": "No more charges required",
    //     "cancelPendingCharges": false
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"closureReason",L"No more charges required");
    CkJsonObjectW_UpdateBool(json,L"cancelPendingCharges",FALSE);

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,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.
    privKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadPemFile(privKey,L"C:/someDir/myAmazonPayPrivateKey.pem");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    // 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
    publicKeyId = L"SANDBOX-AHEGSJCM3L2S637RBGABLAFW";
    success = CkHttpW_SetAuthPrivateKey(http,publicKeyId,privKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

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

    CkHttpW_putAccept(http,L"application/json");

    // Use the actual chargePermissionId here:
    success = CkHttpW_SetUrlVar(http,L"chargePermissionId",L"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
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSb(http,L"DELETE",L"https://pay-api.amazon.eu/sandbox/v2/chargePermissions/{$chargePermissionId}/close",sbRequestBody,L"utf-8",L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkPrivateKeyW_Dispose(privKey);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);
    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    // If the status code is not equal to 200, this will display error information.
    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode != 200) {
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkPrivateKeyW_Dispose(privKey);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    // 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": "Closed",
    //         "reasons":
    //         [{
    //             "reasonCode": null,
    //             "reasonDescription": 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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

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

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"statusDetails.reasons");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        reasonCode = CkJsonObjectW_stringOf(jResp,L"statusDetails.reasons[i].reasonCode");
        reasonDescription = CkJsonObjectW_stringOf(jResp,L"statusDetails.reasons[i].reasonDescription");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkPrivateKeyW_Dispose(privKey);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }