Unicode C
Unicode C
Paynow.pl -- Make a Payment Request
See more Paynow.pl Examples
Make a payment request POST with prepared message on Paynow payment request endpoint.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkCrypt2W.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW json;
const wchar_t *myApiAccessKey;
const wchar_t *mySigCalcKey;
HCkCrypt2W crypt;
const wchar_t *messageBody;
const wchar_t *signature;
HCkHttpResponseW resp;
HCkJsonObjectW jsonResp;
const wchar_t *redirectUrl;
const wchar_t *paymentId;
const wchar_t *status;
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 --request POST 'https://api.sandbox.paynow.pl/v1/payments' \
// -H 'Content-Type: application/json' \
// -H 'Api-Key: c12c386b-650b-43db-9430-d84fc05d9433' \
// -H 'Signature: aYPCytCoc+/wFgqHZJjgBCi20omXTn0yzm9LysJgnFo=' \
// -H 'Idempotency-Key: 59c6dd26-f905-487b-96c9-fd1d2bd76885' \
// --data-raw '{
// "amount": 45671,
// "externalId": "234567898654",
// "description": "Test transaction",
// "buyer": {
// "email": "jan.kowalski@melements.pl"
// }
// }'
// 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.
// {
// "amount": 45671,
// "externalId": "234567898654",
// "description": "Test transaction",
// "buyer": {
// "email": "jan.kowalski@melements.pl"
// }
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateInt(json,L"amount",45671);
CkJsonObjectW_UpdateString(json,L"externalId",L"234567898654");
CkJsonObjectW_UpdateString(json,L"description",L"Test transaction");
CkJsonObjectW_UpdateString(json,L"buyer.email",L"jan.kowalski@melements.pl");
myApiAccessKey = L"c12c386b-650b-43db-9430-d84fc05d9433";
mySigCalcKey = L"b758f20d-ba92-44fa-acca-f57e99787b9d";
// Calculate the Signature header.
crypt = CkCrypt2W_Create();
CkCrypt2W_putMacAlgorithm(crypt,L"hmac");
CkCrypt2W_putEncodingMode(crypt,L"base64");
CkCrypt2W_SetMacKeyString(crypt,mySigCalcKey);
CkCrypt2W_putHashAlgorithm(crypt,L"SHA-256");
messageBody = CkJsonObjectW_emit(json);
signature = CkCrypt2W_macStringENC(crypt,messageBody);
CkHttpW_SetRequestHeader(http,L"Idempotency-Key",CkCrypt2W_generateUuid(crypt));
CkHttpW_SetRequestHeader(http,L"Api-Key",myApiAccessKey);
CkHttpW_SetRequestHeader(http,L"Signature",signature);
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
CkHttpW_putAccept(http,L"application/json");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.sandbox.paynow.pl/v1/payments",json,L"application/json",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"Response body:\n");
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
// Sample response:
// {
// "redirectUrl": "https://paywall.sandbox.paynow.pl/NOA0-YJ9-Y1P-29V?token=eyJraWQiOiJhMD ... L60wk",
// "paymentId": "NOA0-YJ9-Y1P-29V",
// "status": "NEW"
// }
jsonResp = CkJsonObjectW_Create();
CkJsonObjectW_Load(jsonResp,CkHttpResponseW_bodyStr(resp));
redirectUrl = CkJsonObjectW_stringOf(json,L"redirectUrl");
paymentId = CkJsonObjectW_stringOf(json,L"paymentId");
status = CkJsonObjectW_stringOf(json,L"status");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(jsonResp);
}