Sample code for 30+ languages & platforms
Unicode C

Amazon Pay - Get Checkout Session

See more Amazon Pay Examples

Get Checkout Session details includes buyer info, payment instrument details, and shipping address.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkPrivateKeyW privKey;
    const wchar_t *publicKeyId;
    int respStatusCode;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    const wchar_t *paymentDescriptor;
    const wchar_t *constraintId;
    const wchar_t *description;
    const wchar_t *strVal;
    const wchar_t *checkoutSessionId;
    const wchar_t *webCheckoutDetailsCheckoutReviewReturnUrl;
    const wchar_t *webCheckoutDetailsCheckoutResultReturnUrl;
    const wchar_t *webCheckoutDetailsCheckoutCancelUrl;
    const wchar_t *webCheckoutDetailsAmazonPayRedirectUrl;
    const wchar_t *chargePermissionType;
    const wchar_t *recurringMetadataFrequencyUnit;
    const wchar_t *recurringMetadataFrequencyValue;
    const wchar_t *recurringMetadataAmountAmount;
    const wchar_t *recurringMetadataAmountCurrencyCode;
    const wchar_t *productType;
    const wchar_t *paymentDetailsPaymentIntent;
    BOOL paymentDetailsCanHandlePendingAuthorization;
    const wchar_t *paymentDetailsChargeAmount;
    const wchar_t *paymentDetailsTotalOrderAmount;
    const wchar_t *paymentDetailsSoftDescriptor;
    const wchar_t *paymentDetailsPresentmentCurrency;
    const wchar_t *paymentDetailsAllowOvercharge;
    const wchar_t *paymentDetailsExtendExpiration;
    const wchar_t *merchantMetadataMerchantReferenceId;
    const wchar_t *merchantMetadataMerchantStoreName;
    const wchar_t *merchantMetadataNoteToBuyer;
    const wchar_t *merchantMetadataCustomInformation;
    const wchar_t *supplementaryData;
    const wchar_t *buyerBuyerId;
    const wchar_t *buyerName;
    const wchar_t *buyerEmail;
    const wchar_t *buyerPhoneNumber;
    const wchar_t *buyerPrimeMembershipTypes;
    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 *statusDetailsState;
    const wchar_t *statusDetailsReasonCode;
    const wchar_t *statusDetailsReasonDescription;
    const wchar_t *statusDetailsLastUpdatedTimestamp;
    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 *platformId;
    const wchar_t *chargePermissionId;
    const wchar_t *chargeId;
    const wchar_t *creationTimestamp;
    const wchar_t *expirationTimestamp;
    const wchar_t *storeId;
    const wchar_t *deliverySpecificationsAddressRestrictionsType;
    const wchar_t *providerMetadataProviderReferenceId;
    const wchar_t *releaseEnvironment;
    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/checkoutSessions/:checkoutSessionId"
    //   -X GET
    //   -H "authorization:Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE"
    //   -H "x-amz-pay-date:20201012T235046Z"

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

    // 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);
        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);
        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");

    respStatusCode = 0;

    sbResponseBody = CkStringBuilderW_Create();
    success = CkHttpW_SetUrlVar(http,L"sessionId",L"62b7d028-6c7c-4a43-b077-3897dff27c5b");
    // To use the live system, replace "sandbox" with "live" in the URL passed to QuickGetSb.
    // Also, make sure to use the correct region: pay-api.amazon.com, pay-api.amazon.eu, or pay-api.amazon.jp
    success = CkHttpW_QuickGetSb(http,L"https://pay-api.amazon.eu/sandbox/v2/checkoutSessions/{$sessionId}",sbResponseBody);
    if (success == FALSE) {
        // If the LastStatus is not equal to 0, then we received a response, but it was an error response.
        respStatusCode = CkHttpW_getLastStatus(http);
        if (respStatusCode != 0) {
            wprintf(L"Response Status Code = %d\n",respStatusCode);
            // 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'"}

            wprintf(L"Response body:\n");
            wprintf(L"%s\n",CkHttpW_lastResponseBody(http));
        }
        else {
            wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        }

        CkHttpW_Dispose(http);
        CkPrivateKeyW_Dispose(privKey);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpW_getLastStatus(http);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    // We expect a 200 status code for success.
    // Note: Some Amazon Pay API calls return 200 for success, others return 201.
    if (respStatusCode != 200) {
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkPrivateKeyW_Dispose(privKey);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    // 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
    //   },
    //   "chargePermissionType": "Recurring",
    //   "recurringMetadata": {
    //     "frequency": {
    //       "unit": "Month",
    //       "value": "1"
    //     },
    //     "amount": {
    //       "amount": "30",
    //       "currencyCode": "USD"
    //     }
    //   },
    //   "productType": "PayAndShip",
    //   "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": {
    //     "buyerId": "buyerId",
    //     "name": "name-1",
    //     "email": "name@amazon.com",
    //     "phoneNumber": "800-000-0000",
    //     "primeMembershipTypes": null
    //   },
    //   "billingAddress": {
    //     "name": "Work",
    //     "addressLine1": "440 Terry Ave",
    //     "addressLine2": "",
    //     "addressLine3": "",
    //     "city": "Seattle",
    //     "county": "King",
    //     "district": "Seattle",
    //     "stateOrRegion": "WA",
    //     "postalCode": "98121",
    //     "countryCode": "US"
    //   },
    //   "paymentPreferences": [
    //     {
    //       "paymentDescriptor": "Your selected Amazon payment method"
    //     }
    //   ],
    //   "statusDetails": {
    //     "state": "Open",
    //     "reasonCode": null,
    //     "reasonDescription": null,
    //     "lastUpdatedTimestamp": "20191015T204327Z"
    //   },
    //   "shippingAddress": {
    //     "name": "Susie Smith",
    //     "addressLine1": "10 Ditka Ave",
    //     "addressLine2": "Suite 2500",
    //     "addressLine3": null,
    //     "city": "Chicago",
    //     "county": null,
    //     "district": null,
    //     "stateOrRegion": "IL",
    //     "postalCode": "60602",
    //     "countryCode": "US",
    //     "phoneNumber": "800-000-0000"
    //   },
    //   "platformId": null,
    //   "chargePermissionId": null,
    //   "chargeId": null,
    //   "constraints": [
    //     {
    //       "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

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

    checkoutSessionId = CkJsonObjectW_stringOf(jResp,L"checkoutSessionId");
    webCheckoutDetailsCheckoutReviewReturnUrl = CkJsonObjectW_stringOf(jResp,L"webCheckoutDetails.checkoutReviewReturnUrl");
    webCheckoutDetailsCheckoutResultReturnUrl = CkJsonObjectW_stringOf(jResp,L"webCheckoutDetails.checkoutResultReturnUrl");
    webCheckoutDetailsCheckoutCancelUrl = CkJsonObjectW_stringOf(jResp,L"webCheckoutDetails.checkoutCancelUrl");
    webCheckoutDetailsAmazonPayRedirectUrl = CkJsonObjectW_stringOf(jResp,L"webCheckoutDetails.amazonPayRedirectUrl");
    chargePermissionType = CkJsonObjectW_stringOf(jResp,L"chargePermissionType");
    recurringMetadataFrequencyUnit = CkJsonObjectW_stringOf(jResp,L"recurringMetadata.frequency.unit");
    recurringMetadataFrequencyValue = CkJsonObjectW_stringOf(jResp,L"recurringMetadata.frequency.value");
    recurringMetadataAmountAmount = CkJsonObjectW_stringOf(jResp,L"recurringMetadata.amount.amount");
    recurringMetadataAmountCurrencyCode = CkJsonObjectW_stringOf(jResp,L"recurringMetadata.amount.currencyCode");
    productType = CkJsonObjectW_stringOf(jResp,L"productType");
    paymentDetailsPaymentIntent = CkJsonObjectW_stringOf(jResp,L"paymentDetails.paymentIntent");
    paymentDetailsCanHandlePendingAuthorization = CkJsonObjectW_BoolOf(jResp,L"paymentDetails.canHandlePendingAuthorization");
    paymentDetailsChargeAmount = CkJsonObjectW_stringOf(jResp,L"paymentDetails.chargeAmount");
    paymentDetailsTotalOrderAmount = CkJsonObjectW_stringOf(jResp,L"paymentDetails.totalOrderAmount");
    paymentDetailsSoftDescriptor = CkJsonObjectW_stringOf(jResp,L"paymentDetails.softDescriptor");
    paymentDetailsPresentmentCurrency = CkJsonObjectW_stringOf(jResp,L"paymentDetails.presentmentCurrency");
    paymentDetailsAllowOvercharge = CkJsonObjectW_stringOf(jResp,L"paymentDetails.allowOvercharge");
    paymentDetailsExtendExpiration = CkJsonObjectW_stringOf(jResp,L"paymentDetails.extendExpiration");
    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");
    supplementaryData = CkJsonObjectW_stringOf(jResp,L"supplementaryData");
    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");
    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");
    statusDetailsState = CkJsonObjectW_stringOf(jResp,L"statusDetails.state");
    statusDetailsReasonCode = CkJsonObjectW_stringOf(jResp,L"statusDetails.reasonCode");
    statusDetailsReasonDescription = CkJsonObjectW_stringOf(jResp,L"statusDetails.reasonDescription");
    statusDetailsLastUpdatedTimestamp = CkJsonObjectW_stringOf(jResp,L"statusDetails.lastUpdatedTimestamp");
    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");
    platformId = CkJsonObjectW_stringOf(jResp,L"platformId");
    chargePermissionId = CkJsonObjectW_stringOf(jResp,L"chargePermissionId");
    chargeId = CkJsonObjectW_stringOf(jResp,L"chargeId");
    creationTimestamp = CkJsonObjectW_stringOf(jResp,L"creationTimestamp");
    expirationTimestamp = CkJsonObjectW_stringOf(jResp,L"expirationTimestamp");
    storeId = CkJsonObjectW_stringOf(jResp,L"storeId");
    deliverySpecificationsAddressRestrictionsType = CkJsonObjectW_stringOf(jResp,L"deliverySpecifications.addressRestrictions.type");
    providerMetadataProviderReferenceId = CkJsonObjectW_stringOf(jResp,L"providerMetadata.providerReferenceId");
    releaseEnvironment = CkJsonObjectW_stringOf(jResp,L"releaseEnvironment");
    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"constraints");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        constraintId = CkJsonObjectW_stringOf(jResp,L"constraints[i].constraintId");
        description = CkJsonObjectW_stringOf(jResp,L"constraints[i].description");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"deliverySpecifications.specialRestrictions");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"deliverySpecifications.specialRestrictions[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"deliverySpecifications.addressRestrictions.restrictions.US.statesOrRegions[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"deliverySpecifications.addressRestrictions.restrictions.US.zipCodes");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"deliverySpecifications.addressRestrictions.restrictions.US.zipCodes[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"deliverySpecifications.addressRestrictions.restrictions.GB.zipCodes[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"deliverySpecifications.addressRestrictions.restrictions.IN.statesOrRegions[i]");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkPrivateKeyW_Dispose(privKey);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }