ตัวอย่างการเชื่อมระบบกับ PAYSBUY API ด้วย ASP.NET (C#)

ตัวอย่างการเชื่อมระบบกับ PAYSBUY API ด้วย ASP.NET (C#)

    • Submitted By:
    • PAYSBUY Dev.
    • Products Used:
    • PAYSBUY API
    • ภาษา:
    • ASP.NET (C#)
    • วันที่:
    • 25 สิงหาคม 2552
  1. ในการเชื่อมระบบกับ PAYSBUY API คุณสามารถ Download Code ตัวอย่างได้ ที่นี่
  2. เมื่อคุณแตกไฟล์จะพบไฟล์ ดังต่อไปนี้
    • ไฟล์ Default.aspx.cs ใช้ตรวจสอบสิทธิ์การใช้งานระหว่างร้านค้าของคุณกับระบบ PAYSBUY โดยมีรายละเอียดดังต่อไปนี้

      using System;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web; using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;

      public partial class _Default : System.Web.UI.Page

      {
      protected void Page_Load(object sender, EventArgs e)

      {
      string strPSBID = "หมายเลขเพย์สบายของคุณ";
      string strUsername = "ชื่ออีเมลล์ของคุณที่สมัครเพย์สบาย";
      string strSecureCode = "Secure Code ของคุณ";
      string strInv = "หมายเลขสินค้าของคุณ";
      string strItm = "รายละเอียดสินค้าของคุณ";
      double dbAtm = "ราคาสินค้า";
      string strCurr_type = "TH";
      string strCom = "kfLapVmkZFJ58LwwHQZZzg33EzW9MWKXK0vjS0XXiFQ="; //Comission Code ของคุณ
      string strMethod = "1"; // 1 = รับเงินจากบัญชีเพย์สบาย , 2 = รับเงินจาก Credit Card;
      string strLanguage = "T";
      string strResp_front_url = "URL ของคุณที่รับค่าจาก PAYSBUY ทาง Frontend";
      string strResp_back_url = "URL ของคุณที่รับค่าจาก PAYSBUY ทาง Backend";

      paynow.api_paynow psb = new paynow.api_paynow();

      //1. ก่อนเรียกใช้งาน ต้องทำการ Authentication โดยเรียก method api_paynow_authentication

      string strStatus = psb.api_paynow_authentication(strPSBID, strUsername, strPassword, strSecureCode, strInv, strItm,
      dbAtm, strCurr_type, strCom, strMethod, strLanguage, strResp_front_url, strResp_back_url);

      Label1.Text = strStatus;

      string strApproveCode = strStatus.Substring(0, 2);

      int intLen = strStatus.Length;
      string strRef = strStatus.Substring(2, intLen-2);

      Session["Ref"] = strRef;
      Session["Inv"] = strInv;

      //2. หาก Authen ผ่าน Paysbuy จะ Return 00 ให้ Redirect ไปที่หน้า API Page

      if (strApproveCode == "00")
      {

      Response.Redirect("http://demo.paysbuy.com/api_payment/paynow.aspx?securecode=" + strSecureCode + "&refid=" + strRef + "");
      }
      else
      {
      // Fail Page

      Label1.Text = "strApproveCode->" + strApproveCode;
      Label2.Text = "
      strRef ->" + strRef;
      }
      }

      }

    • ไฟล์ receive_back.aspx.cs เป็นโค๊ด ASP.NET ที่ใช้รับค่าการชำระเงินจากทาง PAYSBUY ทาง Backend โดยมีรายละเอียดดังต่อไปนี้

      using System;
      using System.Collections;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.IO;

      public partial class receive_back : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {

      StreamWriter StrWer = default(StreamWriter);

      try {

      string strResult = (Request.Form["result"]).Trim();
      string strapCode = (Request.Form["apCode"]).Trim();
      string strAmt = (Request.Form["amt"]).Trim();
      string strMethod = (Request.Form["method"]).Trim();

      //สร้าง Folder ชื่อ PaymentLog

      StrWer = File.CreateText(Server.MapPath("PaymentLog/") + strapCode+"_log.txt");
      StrWer.Write(strResult + "#" + strapCode + "#" + strAmt+ "#"+strMethod);
      StrWer.Close();

      }

      catch (Exception ex) {

      }
      }
      }

    • ไฟล์ receive_front.aspx.cs เป็นโค๊ด ASP.NET ที่ใช้รับค่าการชำระเงินจากทาง PAYSBUY ทาง Frontend แล้วนำค่าที่ได้ ไปตรวจสอบกับค่าที่ได้จาก Backend โดยมีรายละเอียดดังต่อไปนี้

      using System;
      using System.Collections;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web; using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Xml.Linq;
      using System.IO;

      public partial class receive_front : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {
      string strResult = "", strapCode = "", strAmt = "", strMethod="",strPayment="";

      try{

      //1. รับค่าจากทาง Paysbuy โดย POST Method

      strResult = (Request.Form["result"]).Trim();
      strapCode = (Request.Form["apCode"]).Trim();
      strAmt = (Request.Form["amt"]).Trim();
      strMethod = (Request.Form["method"]).Trim();

      string strRefid = Convert.ToString(Session["Ref"]);
      string strInv = Convert.ToString(Session["Inv"]);

      //2. ตรวจสอบค่าที่ได้จาก Backend Process ที่เขียนเป็น Text File ไว้แล้ว

      if (CheckStatusPayment(strResult, strapCode, strAmt, strMethod))
      {
      // ข้อมูลการชำระเงินตรงกัน ส่งสินค้าให้ลูกค้าได้

      strPayment += "Backend Process ->True";
      }
      else
      {
      // ข้อมูลการชำระเงินไม่ตรงกัน

      strPayment += " Backend Process ->False";
      }

      Label1.Text = strPayment;

      }
      catch
      {
      Label1.Text = "Not receive data from PAYSBUY";
      }

      private Boolean CheckStatusPayment(string strResult, string strApCode, string strAmt, string strMethod)

      {

      StreamReader StrWer;

      Boolean blCheck = true;

      String strLine, strResultFile="", strApCodeFile="", strAmtFile="", strMethodFile="";

      StrWer = File.OpenText(Server.MapPath("PaymentLog/" + strApCode + "_log.txt"));

      while (StrWer.EndOfStream == false)

      {
      strLine = StrWer.ReadLine();

      if(strLine.Trim() != "")
      {
      strResultFile = strLine.Split('#')[0];
      strApCodeFile = strLine.Split('#')[1];
      strAmtFile = strLine.Split('#')[2];
      strMethodFile = strLine.Split('#')[3];
      }
      }

      StrWer.Close();

      if (strResultFile != strResult){
      blCheck = false;
      }

      if (strApCodeFile != strApCode){
      blCheck = false;
      }

      if (strAmtFile != strAmt){
      blCheck = false;
      }

      return blCheck; //*** Return Boolean ***//

      }
      }

« กลับหน้าบทเรียนและวิธีการเขียนโปรแกรม