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

Clickatell Chat 2 Pay: Send a payment link for an order

See more Clickatell Examples

Use a configured Chat 2 Pay application and its own API key. Supply the application ID, an unpaid order, and a recipient on the configured SMS channel. Clickatell creates and sends the checkout link. HTTP 201 returns a paymentLinkRef that can be saved on the order. Keep the order unpaid pending confirmation of payment.

The example writes a local reference record for the sample order. After an uncertain network result, check the transaction in Clickatell before creating another link.

Clickatell API reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Handle hoNow
    Boolean iSuccess
    String sOrderNumber
    Variant vBody
    Handle hoBody
    Variant vResp
    Handle hoResp
    Boolean iSent
    Handle hoReply
    Boolean iParsed
    String sReference
    Handle hoRecord
    Boolean iSaved
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True

    Send ComSetRequestHeader To hoHttp "Authorization" "CHAT_2_PAY_API_KEY"
    Send ComSetRequestHeader To hoHttp "Accept" "application/json"
    Get Create (RefClass(cComCkDateTime)) To hoNow
    If (Not(IsComObjectCreated(hoNow))) Begin
        Send CreateComObject of hoNow
    End
    Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
    Move "ORD-1042" To sOrderNumber
    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComUpdateString Of hoBody "applicationId" "CHAT_2_PAY_APPLICATION_ID" To iSuccess
    Get ComUpdateString Of hoBody "channel" "sms" To iSuccess
    Get ComUpdateString Of hoBody "to" "PHONE_NUMBER" To iSuccess
    Get ComUpdateString Of hoBody "orderNumber" sOrderNumber To iSuccess
    Get ComGetAsTimestamp Of hoNow False To sTemp1
    Get ComUpdateString Of hoBody "timestamp" sTemp1 To iSuccess
    Get ComUpdateString Of hoBody "currency" "USD" To iSuccess
    Get ComUpdateNumber Of hoBody "totalAmount" "49.95" To iSuccess
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://chat2pay-service.clickatell.com/api/v2/chat-2-pay" vBody "application/json" vResp To iSent
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No HTTP response. Check this order in Chat 2 Pay before retrying."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoReply sTemp1 To iParsed
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSent <> True) Or (iTemp1 <> 201)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Payment-link request returned HTTP " iTemp1
        Get ComHasMember Of hoReply "message" To bTemp1
        If (bTemp1 = True) Begin
            Get ComStringOf Of hoReply "message" To sTemp1
            Showln sTemp1
        End

        Procedure_Return
    End

    Get ComStringOf Of hoReply "paymentLinkRef" To sReference
    Get ComJsonTypeOf Of hoReply "paymentLinkRef" To iTemp1
    If ((iParsed <> True) Or (iTemp1 <> 1) Or (sReference = "")) Begin
        Showln "Missing payment link reference. Check the transaction before retrying."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoRecord
    If (Not(IsComObjectCreated(hoRecord))) Begin
        Send CreateComObject of hoRecord
    End
    Get ComUpdateString Of hoRecord "orderNumber" sOrderNumber To iSuccess
    Get ComUpdateString Of hoRecord "paymentLinkRef" sReference To iSuccess
    Get ComJsonTypeOf Of hoReply "transactionStatus" To iTemp1
    If (iTemp1 = 1) Begin
        Get ComStringOf Of hoReply "transactionStatus" To sTemp1
        Get ComUpdateString Of hoRecord "transactionStatus" sTemp1 To iSuccess
    End

    Get ComWriteFile Of hoRecord "order-1042-payment-link.json" To iSaved
    If (iSaved <> True) Begin
        Showln "Link already created. Save this reference with the order: " sReference
        Procedure_Return
    End

    Showln "Saved the payment link reference for " sOrderNumber


End_Procedure