Unicode C
Unicode C
Amazon Pay - Get Charge Permission
See more Amazon Pay Examples
Get Charge Permission to determine if this Charge Permission can be used to charge the buyer.Chilkat Unicode C Downloads
#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 *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 *statusDetailsReasons;
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"
// -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"chargePermissionId",L"P21-1111111-1111111");
// 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/chargePermissions/{$chargePermissionId}",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);
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)
// {
// "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
// 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");
statusDetailsReasons = CkJsonObjectW_stringOf(jResp,L"statusDetails.reasons");
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;
}
CkHttpW_Dispose(http);
CkPrivateKeyW_Dispose(privKey);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}