Managing transfers with side effects
Managing transfers with side effects in the Blnk Ledger
1. Managing side effects with Refund
When dealing with financial transactions, it’s crucial to handle scenarios where things don’t go as planned. One common situation is when a payment verification from a provider is not successful after funds have been moved.
In such cases, we need to reverse the transaction using Blnk’s refund endpoint.
First, let’s perform the initial transfer:
View your transactions in your terminal:
Handling Webhooks and Automating Refunds
In a real-world scenario, you would typically set up a webhook to receive notifications from your payment provider about the status of transactions. Let’s set up a simple Express.js server to handle the webhook and automate the refund process:
Now, let’s break down this example:
- We set up an Express.js server with a
/payment-webhook
endpoint to receive notifications from the payment provider. - When a webhook is received, we check the status of the payment. If it’s ‘FAILED’, we initiate the refund process.
- The
findTransactionId
function is a placeholder for where you would typically query your database to find the original Blnk transaction ID based on the provider’s reference. In a real-world scenario, you would store this mapping when initiating the original transaction. - The
initiateRefund
function calls the Blnk refund endpoint, similar to our previous example.
To test this setup, you can use a tool like cURL to simulate a webhook from your payment provider:
This cURL command simulates a failed payment notification from your provider.
Best practices for managing side effects and webhook handling
- Immediate Action: Process refunds as soon as you receive notification of a failed verification to ensure good customer experience.
- Detailed Logging: Always include detailed information in the
meta_data
field. This aids in troubleshooting and auditing. - Balance Verification: Always verify the balance after processing a refund to ensure the transaction was successful.
- Error Handling: Implement robust error handling in your refund process. If a refund fails, you may need to retry or escalate to manual intervention.
- Customer Communication: Implement a system to notify customers about the failed transaction and subsequent refund.
- Reconciliation: Regularly reconcile your internal records with Blnk’s transaction and refund logs to ensure accuracy.
- Verify Webhook Authenticity: In a production environment, implement a mechanism to verify that the webhook is genuinely from your payment provider. This often involves checking a signature or secret key.
- Idempotency: Ensure your webhook handler is idempotent. Providers may send the same webhook multiple times, so your system should handle duplicate notifications gracefully.
- Asynchronous Processing: For high-volume systems, consider processing webhooks asynchronously. You can acknowledge receipt immediately and process the webhook contents in a background job.
- Monitoring: Set up monitoring and alerting for your webhook endpoint. This can help you quickly identify and respond to any issues in the payment verification and refund process.
When you run this command, you should see output in your server logs similar to:
2. Managing side effects with Inflight
Example: Using Inflight for a payment requiring additional information
Let’s consider a scenario where we need to transfer $750.00 from Customer A’s USD wallet to an external payment provider, but the payment requires additional verification before being finalized. We’ll use the Inflight feature to manage this process.
First, let’s initiate the transaction with the Inflight flag:
View your transactions in your terminal:
Handling webhooks and automating inflight transaction resolution
Let’s set up a simple Express.js server to handle webhooks and automate the Inflight transaction resolution process:
This example demonstrates:
- Setting up a webhook endpoint to receive notifications from your payment provider.
- Processing the webhook based on the payment status (VERIFIED or REJECTED).
- Committing or voiding the Inflight transaction accordingly.
- Error handling and logging.
Using expiry dates with inflight transactions
Expiration dates are crucial for managing Inflight transactions effectively. They ensure that transactions don’t remain in a pending state indefinitely, which could potentially lock funds unnecessarily.
Example: Setting and handling expiration
Blnk automatically voids the transaction once the expiration date is met and the transaction has not been committed fully.
Implementing partial commits for inflight transactions
Partial commits allow you to finalize a portion of an Inflight transaction while keeping the remainder in the Inflight state. This is particularly useful for scenarios where a transaction may be processed in parts.
Example: Partial commit of an inflight transaction
In this example:
- We defined a
partialCommitInflightTransaction
function that commits only a portion of the Inflight transaction. - The
handlePartialPaymentVerification
function demonstrates how you might use partial commits in your payment flow:- It commits the verified amount.
- Updates internal records.
- Checks if there’s any remaining amount still in the Inflight state.
- The
checkRemainingInflightAmount
function (which you would implement to query Blnk’s API or your database) helps track the progress of partial commits.
By using partial commits, you can handle complex scenarios where funds are released incrementally, such as:
- Multi-stage payment processes.
- Partial fulfillment of orders.
- Gradual release of funds based on milestone completions in a project.
Best practices for managing side effects with Inflight
- Balance Checks: Ensure the source balance has enough funds to complete the transaction. Prevent the source balance from having a balance lower than the amount in its inflight_debit_balance.
- Available Balance Calculation: This prevents users from accessing funds that are held in Inflight transactions. In your application, calculate the available balance as:
- Error Handling: Implement robust error handling in your Inflight process. If a commit or void operation fails, you may need to retry or escalate to manual intervention.
- Customer Communication: Implement a system to notify customers about the status of their transactions, especially when they’re held in Inflight.
- Reconciliation: Regularly reconcile your internal records with Blnk’s transaction logs to ensure accuracy, paying special attention to Inflight transactions.(Blnk v1 will support reconcillation features built into the ledger).
- Webhook Authentication: In a production environment, implement a mechanism to verify that the webhook is genuinely from your payment provider.
- Idempotency: Ensure your webhook handler is idempotent. Providers may send the same webhook multiple times, so your system should handle duplicate notifications gracefully.
- Monitoring: Set up monitoring and alerting for your webhook endpoint and Inflight transactions. This can help you quickly identify and respond to any issues in the payment verification process.
Need help?
We are very happy to help you make the most of Blnk, regardless of whether it is your first time or you are switching from another tool.
To ask questions or discuss issues, please contact us or join our Discord community.
Manage your Blnk Ledger and explore advanced features (access control & collaboration, anomaly detection, secure storage & file management, etc.) in one dashboard.
Was this page helpful?