This article was written following a need to integrate PayPal Express Checkout in a c++ Win32 application.
I was thinking about integration in the background without any website other than the payment page as part of a desktop application in c++. Would it be possible to following the following scenario:
Generate the invoice / sale and via REST API obtain some sort of unique ID for the transaction to come.
Redirect to Paypal web site to a ad-hoc payment page, using the unique ID.
In the background, check every few minutes, via REST API, if the payment was made.
I have found a way and created a Proof of Concept (POC) for a built-in payment processing engine which allows you to accept payments from any credit card holder (regardless of being a PayPal customer) and pay for unlocking a software product or for specific features.
To process payments you need to apply as a PayPal developer and obtain your own PayPal credentials. You will then receive 2 sets of credentials. One for tests ("sandbox") and the other for real life.
First you should test the Sandbox to test the API.
I have created a PayPal class with one "init()" used for both "sandbox" and real life transactions.
When you wish to initiate a payment from your program, you call the following function which I wrote which generally build a string (ExpChkoutStr) and use the following PayPal API call:
For all HTTP communication, I used The WinHTTP class was developed by Cheng Shi.
Here is how I send the initial request to the PayPal servers:
// Send string to PayPal server WinHttpClient WinClient1(ExpChkoutStr.GetBuffer()); WinClient1.SetRequireValidSslCertificates(false); // Now we get PayPal's response: WinClient1.SendHttpRequest(L"GET"); httpResponseContent1 = WinClient1.GetResponseContent(); CString strTransactionRet = UrlDecode(httpResponseContent1.c_str());
As you can see we are sending PayPal a long string we generate using another function. This function combined the credentials, the nature of the requested transaction, additional details all into a single string.
result = (m_sandbox) ? PAYPAL_SANDBOX_HTTPS :
result += Q_USER;
result += m_user;
result += AND_PASSWORD;
result += m_password;
result += AND_SIGNATURE;
result += m_signature;
result += AND_PAYMENTAMOUNT;
result += strAmount;
result += L"&METHOD=SetExpressCheckout";
result += AND_RETURN_URL;
result += m_SuccessURL;
result += AND_CANCEL_URL;
result += m_FailureURL;
result += AND_VERSION;
result += L"&NOSHIPPING=1";
result += L"&ADDROVERRIDE=0&BRANDNAME=Secured Globe, Inc.";
result += L"&PAYMENTREQUEST_0_DESC=";
result += L"Item name: " + strUnits + L"(" + UnitName + L") "; result += L"Price: " + strAmount;
result += L"&NOTETOBUYER=Here you can add a note to the buyer";
Now, result will hold the string to be sent in the previous code block.
We then examine the result we have received back from PayPal:
The result from the PayPal server is a "token" used to figure out a one-time web page (LinkToOpen ) that must be opened in order for the end user to confirm the purchase:
// Extract token from response CString sToken = ExtractElement(strTransactionRet, L"TOKEN");