Skip to content

Getting Started With MolPayCS For .Net Core

Haziman Hashim edited this page Aug 27, 2019 · 2 revisions

NOTE #1: THE DLL LIBRARY IS BEING REVIEWED
NOTE #2: PLEASE BE INFORMED THAT THIS LIBRARY WAS BUILT WITH DOTNET CORE 2.2
NOTE #3: CURRENTLY MADE USABLE FOR ASP.NET CORE MVC

Dependencies

  1. Microsoft.AspNetCore.Html.Abstractions.dll
  2. Microsoft.AspNetCore.Http.Abstractions.dll
  3. Microsoft.AspNetCore.Mvc.ViewFeatures.dll

Installation & Configuration

  1. Add reference to "MolPayCS.Core.dll"
  2. Inject HttpContext into services in Startup.cs
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddSession();                 // <!-- This line enables session
            services.AddHttpContextAccessor();     // <!-- REQUIRED: This line injects HttpContext into the request
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            // Assign the HttpContextAccessor to our Plugin
            MolPayCS.Core.AppContext.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Usage

Controller

use MolPayCS.Core;

Payment pmtObject = new Payment();
// same step with normal DotNet library

ViewBag.pmtObject = pmtObject;
return View();

View

@using MolPayCS.Core;
@using MolPayCS.Core.Helpers;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
  FormHelper.autoSubmit = true; // set to false to prevent auto submit
}

<div>
@using(Payment pmtObject = ViewBag.pmtObject)
{
        <h1>Payment Form</h1>
        @Html.PaymentForm(pmtObject)

        <h1>Requery Transaction ID (Single View)</h1>
        @Html.RequeryTidForm(pmtObject);

        <h1>Requery Multi Transaction ID</h1>
        @Html.RequeryMultiTidForm(pmtObject);

        <h1>Requery Multi Order ID</h1>
        @Html.RequeryMultiOidForm(pmtObject);

        <h1>Requery Order ID (Batch View)</h1>
        @Html.RequeryBatchOidForm(pmtObject);

        <h1>Requery Order ID (Single View)</h1>
        @Html.RequeryOidForm(pmtObject);
}
</div>

Usage: Seamless Payment

Controller

use MolPayCS.Core;

Seamlesspayment paymentObject = new Seamlesspayment();
paymentObject.Merchantid = "xxx";                           //Replace xxx Merchant login username provided by MOLPay
paymentObject.Vkey = "xxx";                                 //Replace xxx with your verify key
paymentObject.Orderid = "xxx";                              //Replace xxx with Bill / Invoice no. provided by merchant
paymentObject.Country = "MY";                               //Buyer country
paymentObject.ReturnUrl = "https://your.url/here";          //Replace xxx with your return URL
paymentObject.CancelUrl = "https://your.url/here";          //Replace xxx with URL to redirect when the payment is time out. Mandatory when timer is enable.
paymentObject.Failureurl = "https://your.url/here";         //Replace xxx with URL to redirect when transcation fail

return Json(
     paymentObject.ProcessRequest()
);

View

  1. Include our seamless Javascript library.
  2. Create a seamless form that sends to the action you made in the controller above.
  3. You are done!