To integrate the MOLPay payment gateway into your ASP.NET MVC web application, you can follow these steps:
- Sign up for a MOLPay account: Visit the MOLPay website (https://www.molpay.com) and sign up for a merchant account. This will provide you with the necessary credentials to interact with the MOLPay API.
- Install the MOLPay SDK: In your ASP.NET MVC project, use NuGet Package Manager to install the MOLPay SDK. You can search for the package named “MOLPayXDK.NET” and install it.
- Configure MOLPay settings: In your web.config file, add the necessary MOLPay settings. You’ll need to provide your MOLPay merchant ID and verify key. Example configuration settings:
<appSettings>
<add key="MOLPay.MerchantID" value="YourMerchantID" />
<add key="MOLPay.VerifyKey" value="YourVerifyKey" />
</appSettings>
- Create a payment form: In your MVC view, create a form to collect payment details such as amount, customer information, and any additional required fields.
- Process the payment request: In your MVC controller, handle the form submission and initiate the payment request to MOLPay. Here’s an example of how you can initiate a payment request:
using MOLPayXDK;
public class PaymentController : Controller
{
public ActionResult MakePayment()
{
// Get payment details from the form submission
decimal amount = // Get the payment amount
string orderId = // Generate a unique order ID
string currency = // Specify the currency code (e.g., MYR)
// Create a new MOLPay instance
MOLPay molpay = new MOLPay();
// Set the MOLPay merchant ID and verify key
molpay.MerchantID = ConfigurationManager.AppSettings["MOLPay.MerchantID"];
molpay.VerifyKey = ConfigurationManager.AppSettings["MOLPay.VerifyKey"];
// Set payment details
molpay.Amount = amount;
molpay.OrderID = orderId;
molpay.CurrencyCode = currency;
// Redirect the user to the MOLPay payment page
return Redirect(molpay.GetPaymentURL());
}
}
In this code, the MakePayment
action method receives the payment details from the form submission. It creates a new instance of the MOLPay
class from the MOLPay SDK and sets the necessary properties such as merchant ID, verify key, payment amount, order ID, and currency code. Finally, the user is redirected to the MOLPay payment page by calling the GetPaymentURL
method.
- Handle the payment response: After the payment is completed, MOLPay will send a response to your specified callback URL. You need to handle this callback in your MVC application to process the payment status and update your system accordingly. You can create an additional action method in your controller to handle the callback and update the payment status in your application’s database.
public class PaymentController : Controller
{
public ActionResult MakePayment()
{
// Payment request code from step 5
}
public ActionResult PaymentCallback()
{
// Handle MOLPay payment callback
MOLPay molpay = new MOLPay();
molpay.VerifyKey = ConfigurationManager.AppSettings["MOLPay.VerifyKey"];
if (molpay.VerifyResponse(Request.Form))
{
// Payment is successful
// Update payment status in your system
}
else
{
// Payment is not
successful
// Handle the failure or error
}
// Return a response to MOLPay (e.g., "OK" or a custom response)
return Content("OK");
}
}
In this code, the PaymentCallback
action method is responsible for handling the callback from MOLPay. The VerifyResponse
method is used to verify the authenticity of the response. If the payment is successful, you can update the payment status in your system accordingly. Finally, you should return a response to MOLPay (typically “OK”) to acknowledge the callback.
Remember to specify the callback URL in your MOLPay account settings so that MOLPay knows where to send the payment response.
These are the basic steps to integrate MOLPay into your ASP.NET MVC web application. You may need to refer to the MOLPay documentation or contact their support for more advanced integration options, customization, and handling additional features like recurring payments or callbacks for different payment statuses.