Xbase++ Requires Chilkat v11.0.0+
Xbase++
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.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oCrypt
LOCAL cRequestId
LOCAL oBody
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsedResult
LOCAL nLinkIndex
nSuccess := 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.
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := "SANDBOX_ACCESS_TOKEN"
oResp := 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.
oCrypt := CreateObject("Chilkat.Crypt2")
cRequestId := oCrypt:GenerateUuid()
oHttp:SetRequestHeader("PayPal-Request-Id", cRequestId)
oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("integration_mode", "LINK")
oBody:UpdateString("type", "BUY_NOW")
oBody:UpdateString("reusable", "MULTIPLE")
oBody:UpdateString("return_url", "https://merchant.example/thank-you")
oBody:UpdateString("line_items[0].name", "Annual inspection")
oBody:UpdateString("line_items[0].unit_amount.currency_code", "USD")
oBody:UpdateString("line_items[0].unit_amount.value", "90.00")
nSuccess := oHttp:HttpJson("POST", "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources", oBody, "application/json", oResp)
IF (nSuccess != 1)
? "No HTTP response: " + oHttp:LastErrorText
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
RETURN
ENDIF
IF ((oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
oErrorInfo := CreateObject("Chilkat.JsonObject")
oErrorInfo:Load(oResp:BodyStr)
? "HTTP " + Str(oResp:StatusCode) + " " + oErrorInfo:StringOf("name") + " " + oErrorInfo:StringOf("details[0].issue")
? "PayPal debug ID: " + oErrorInfo:StringOf("debug_id")
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
RETURN
ENDIF
oResult := CreateObject("Chilkat.JsonObject")
nParsedResult := oResult:Load(oResp:BodyStr)
IF (nParsedResult != 1)
? "Expected a JSON response; HTTP " + Str(oResp:StatusCode)
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
oResult:destroy()
RETURN
ENDIF
? "Catalog payment resource ID: " + oResult:StringOf("id")
// Link order is not fixed. Find the customer-facing link to copy into an email.
nLinkIndex := 0
DO WHILE (nLinkIndex < oResult:SizeOfArray("links"))
oResult:I := nLinkIndex
IF (oResult:StringOfEquals("links[i].rel", "payment_link", 1))
? "Share this payment link: " + oResult:StringOf("links[i].href")
ENDIF
nLinkIndex := nLinkIndex + 1
ENDDO
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
oResult:destroy()