DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoCrypt
String sRequestId
Variant vBody
Handle hoBody
Handle hoErrorInfo
Handle hoResult
Boolean iParsedResult
Integer iLinkIndex
String sTemp1
String sTemp2
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComAuthToken Of hoHttp To "SANDBOX_ACCESS_TOKEN"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
// 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.
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Get ComGenerateUuid Of hoCrypt To sRequestId
Send ComSetRequestHeader To hoHttp "PayPal-Request-Id" sRequestId
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get ComUpdateString Of hoBody "integration_mode" "LINK" To iSuccess
Get ComUpdateString Of hoBody "type" "BUY_NOW" To iSuccess
Get ComUpdateString Of hoBody "reusable" "MULTIPLE" To iSuccess
Get ComUpdateString Of hoBody "return_url" "https://merchant.example/thank-you" To iSuccess
Get ComUpdateString Of hoBody "line_items[0].name" "Annual inspection" To iSuccess
Get ComUpdateString Of hoBody "line_items[0].unit_amount.currency_code" "USD" To iSuccess
Get ComUpdateString Of hoBody "line_items[0].unit_amount.value" "90.00" To iSuccess
Get pvComObject of hoBody to vBody
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources" vBody "application/json" vResp To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln "No HTTP response: " sTemp1
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoErrorInfo
If (Not(IsComObjectCreated(hoErrorInfo))) Begin
Send CreateComObject of hoErrorInfo
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoErrorInfo sTemp1 To iSuccess
Get ComStatusCode Of hoResp To iTemp1
Get ComStringOf Of hoErrorInfo "name" To sTemp1
Get ComStringOf Of hoErrorInfo "details[0].issue" To sTemp2
Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
Get ComStringOf Of hoErrorInfo "debug_id" To sTemp1
Showln "PayPal debug ID: " sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iParsedResult
If (iParsedResult <> True) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Expected a JSON response; HTTP " iTemp1
Procedure_Return
End
Get ComStringOf Of hoResult "id" To sTemp1
Showln "Catalog payment resource ID: " sTemp1
// Link order is not fixed. Find the customer-facing link to copy into an email.
Move 0 To iLinkIndex
While (iLinkIndex < (ComSizeOfArray(hoResult, "links")))
Set ComI Of hoResult To iLinkIndex
Get ComStringOfEquals Of hoResult "links[i].rel" "payment_link" True To bTemp1
If (bTemp1) Begin
Get ComStringOf Of hoResult "links[i].href" To sTemp1
Showln "Share this payment link: " sTemp1
End
Move (iLinkIndex + 1) To iLinkIndex
Loop
End_Procedure