Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

PayPal: Create a reusable payment link for a service

See more PayPal Examples

Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.

PayPal documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loCrypt
LOCAL lcRequestId
LOCAL loBody
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsedResult
LOCAL lnLinkIndex

lnSuccess = 0

*  See Unlock Chilkat before making requests.
*  For the merchant's internal backoffice application. Load the sandbox token
*  from the merchant's credential store. Keep it out of publicly distributed apps.
*  See Get a PayPal access token.
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
loResp = CreateObject('Chilkat.HttpResponse')

*  Replace the thank-you URL with your business website. The same link can be shared
*  with several customers.
*  Create and persist this key once per business operation. On a timeout,
*  retry the same request with its saved key, not a newly generated one.
loCrypt = CreateObject('Chilkat.Crypt2')
lcRequestId = loCrypt.GenerateUuid()
loHttp.SetRequestHeader("PayPal-Request-Id",lcRequestId)
loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateString("integration_mode","LINK")
loBody.UpdateString("type","BUY_NOW")
loBody.UpdateString("reusable","MULTIPLE")
loBody.UpdateString("return_url","https://merchant.example/thank-you")
loBody.UpdateString("line_items[0].name","Annual inspection")
loBody.UpdateString("line_items[0].unit_amount.currency_code","USD")
loBody.UpdateString("line_items[0].unit_amount.value","90.00")

lnSuccess = loHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v1/checkout/payment-resources",loBody,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loCrypt
    RELEASE loBody
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loErrorInfo = CreateObject('Chilkat.JsonObject')
    loErrorInfo.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loErrorInfo.StringOf("name") + " " + loErrorInfo.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loErrorInfo.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loCrypt
    RELEASE loBody
    RELEASE loErrorInfo
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
lnParsedResult = loResult.Load(loResp.BodyStr)
IF (lnParsedResult <> 1) THEN
    ? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
    RELEASE loHttp
    RELEASE loResp
    RELEASE loCrypt
    RELEASE loBody
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

? "Catalog payment resource ID: " + loResult.StringOf("id")
*  Link order is not fixed. Find the customer-facing link to copy into an email.
lnLinkIndex = 0
DO WHILE (lnLinkIndex < loResult.SizeOfArray("links"))
    loResult.I = lnLinkIndex
    IF (loResult.StringOfEquals("links[i].rel","payment_link",1)) THEN
        ? "Share this payment link: " + loResult.StringOf("links[i].href")
    ENDIF

    lnLinkIndex = lnLinkIndex + 1
ENDDO

RELEASE loHttp
RELEASE loResp
RELEASE loCrypt
RELEASE loBody
RELEASE loErrorInfo
RELEASE loResult