Simple record: UnionPay online payment

Source: Internet
Author: User

1. Download the DLL file of chinapay and decompress it to the bin directory. There are 4 files in total.

 

2. Reference chinapay_net.dll to the project.

3. Put the applied key to the website. In this example, put it in the root directory key Folder:

4. Add a class:

Using system; using system. text; using system. web; using chinapay_net; /// <summary> /// chinapay summary /// </Summary> public class chinapay {Private Static string _ merid = "808080 ******** *"; // merchant account Private Static string _ Action = "https://payment.ChinaPay.com/pay/TransGet"; Private Static string _ bgreturl = "http://www.XXX.com/Payment/ChinaPayBgRetUrl.aspx"; Private Static string _ pagereturl = "http://www.X Xx.com/payment/chinapaypagereturl.aspx "; string strurl = httpcontext. current. request. physicalapplicationpath; // <summary> // create a submission form /// </Summary> /// <Param name = "ordid"> order number, length: 11 </param> // <Param name = "amount"> payment amount, retain up to two valid numbers </param> // <returns> </returns> Public static string getchinapayform (string orderid, decimal amount, string privatevalue) {string curyid = "156"; string transdate = d Atetime. Now. tostring ("yyyymmdd"); string transtype = "0001"; stringbuilder sb = new stringbuilder (); If (orderid. length! = 11) {sb. append ("the order number must be 11 characters long");} else if (amount <= 0) {sb. append ("order amount must be greater than 0");} else {string ordid = orderid. substring (0, 4) + _ merid. substring (10, 5) + orderid. substring (4, 7); string transamt = "000000000000" + math. round (amount, 2, midpointrounding. awayfromzero ). tostring (). replace (". "," "); transamt = transamt. substring (transamt. length-12, 12); chinapay CP = new chinapay (); string chkvalue = CP. getsign (_ merid, ordid, transamt, curyid, transdate, transtype); sb. append ("<form action = \" "+ _ Action +" \ "method = \" Post \ ">"); sb. append ("<input type = \" Hidden \ "name = \" merid \ "value = \" "+ _ merid +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" ordid \ "value = \" "+ ordid +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" transamt \ "value = \" "+ transamt +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" curyid \ "value = \" "+ curyid +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" transdate \ "value = \" "+ transdate +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" transtype \ "value = \" "+ transtype +" \ "/>"); sb. append ("<input type = \" hid \ "name = \" version \ "value = \" 20040916 \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" bgreturl \ "value = \" "+ _ bgreturl +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" pagereturl \ "value = \" "+ _ pagereturl +" \ "/>"); sb. append ("<input type = \" Hidden \ "name = \" Upload ID \ "value = \" \ ">"); sb. append ("<input type = \" Hidden \ "name = \" priv1 \ "value = \" "+ privatevalue +" \ ">"); sb. append ("<input type = \" Hidden \ "name = \" chkvalue \ "value = \" "+ chkvalue +" \ ">"); sb. append ("<input id = \" postdata \ "type = \" Submit \ "value = \" Post \ "style = \" display: none; \ "/>"); sb. append ("</form>"); sb. append ("<SCRIPT type = \" text/JavaScript \ "> document. getelementbyid (\ "postdata \"). click (); </SCRIPT> ");} return sb. tostring ();} Public String getsign (string merid, string ordid, string transamt, string curyid, string transdate, string transtype) {netpayclientclass NPC = new netpayclientclass (); string Path = strurl + "key \ merprk. key "; NPC. setmerkeyfile (PATH); string strchkvalue = ""; strchkvalue = NPC. sign (merid, ordid, transamt, curyid, transdate, transtype); Return strchkvalue. trim ();} Public String signdata (string merid, string signmsg) {netpayclientclass NPC = new netpayclientclass (); NPC. setmerkeyfile (strurl + "key \ merprk. key "); string strchkvaluedata =" "; strchkvaluedata = NPC. signdata (merid, signmsg); Return strchkvaluedata. trim ();} public bool getcheck (string merid, string ordid, string transamt, string curyid, string transdate, string transtype, string orderstatus, string checkvalue) {netpayclientclass NPC = new netpayclientclass (); NPC. setpubkeyfile (strurl + "key \ pgpubk. key "); string strflag =" "; bool bolflag = false; strflag = NPC. check (merid, ordid, transamt, curyid, transdate, transtype, orderstatus, checkvalue); If (strflag = "0") {bolflag = true;} return bolflag ;} public bool checkdata (string plaindata, string checkvalue) {netpayclientclass NPC = new netpayclientclass (); NPC. setpubkeyfile (strurl + "key \ pgpubk. key "); string strflagdata =" "; bool bolflagdata = false; strflagdata = NPC. checkdata (plaindata, checkvalue); If (strflagdata = "true") {bolflagdata = true;} return bolflagdata ;}}

5. Add the page. The background code is as follows:

Using system; public partial class payment_chinapaypost {private string orderid = requeststring ("orderid"); // required private decimal amount = requestdecimal ("amount "); // required private string privatevalue = requeststring ("privatevalue"); // optional parameter protected void page_load (Object sender, eventargs e) {string postdata = chinapay. getchinapayform (orderid, amount, privatevalue); responsewrite (postdata );}}

6. Receive the return value:

Using system; /// <summary> /// Brief description of chinapaymodel /// </Summary> public class chinapaymodel1 {/// <summary> // merchant ID /// </ summary> Public String merid {Get; set ;}//< summary> /// Order No. /// </Summary> Public String orderno {Get; set ;} /// <summary> /// Transaction date /// </Summary> Public String transdate {Get; set ;} /// <summary> /// amount /// </Summary> Public String amount {Get; set ;} /// <summary> // currency // </Summary> Public String currencycode {Get; set ;} /// <summary> /// type /// </Summary> Public String transtype {Get; set ;} /// <summary> /// status /// </Summary> Public String status {Get; set ;} /// <summary> /// signature /// </Summary> Public String checkvalue {Get; set ;} /// <summary> /// Payment Gateway number /// </Summary> Public String consumer ID {Get; set ;} /// <summary> /// private domain of the merchant /// </Summary> Public String priv1 {Get; Set ;}} public class chinapaymodel2 {// <summary> // merchant ID // </Summary> Public String merid {Get; set ;} /// <summary> /// Order No. /// </Summary> Public String orderno {Get; set ;} /// <summary> /// Transaction date /// </Summary> Public datetime transdate {Get; set ;} /// <summary> // transaction amount // </Summary> Public decimal amount {Get; Set ;}/// <summary> // currency, 156 is RMB /// </Summary> Public int currencycode {Get; Set ;}/// <summary> // type: 0001 is payment, 0002 /// </Summary> Public String transtype {Get; Set ;}/// <summary> // status: success and Failure // </Summary> Public bool status {Get; set ;} /// <summary> /// whether the signature is correct /// </Summary> Public bool checkvalue {Get; set ;} /// <summary> /// Payment Gateway number /// </Summary> Public String consumer ID {Get; set ;} /// <summary> /// private domain of the merchant /// </Summary> Public String priv1 {Get; Set ;}} public class chinapaymode {Public String requestform {Get; set;} Public String requestquerystring {Get; set;} public chinapaymodel1 model1 {Get; set;} public chinapaymodel2 model2 {Get; set ;}}
Public static chinapaymodel1 chinapaymodel1 () {func <string, string> request = requeststring; string merid = request ("merid"); string orderno = request ("orderno "); string transdate = request ("transdate"); string amount = request ("amount"); string currencycode = request ("currencycode"); string transtype = request ("transtype "); string status = request ("status"); string checkvalue = request ("checkvalue"); string Region ID = request ("Region ID"); string priv1 = request ("priv1 "); return new chinapaymodel1 () {merid = merid, orderno = orderno, transdate = transdate, amount = amount, currencycode = currencycode, transtype = transtype, status = status, checkvalue = checkvalue, required id = required ID, priv1 = priv1};} public static chinapaymodel2 chinapaymodel2 () {func <string, int> parseint = parseint; func <string, decimal> parsedecimal = pagebaseuser. parsedecimal; chinapaymodel1 model = chinapaymodel1 (); string orderno = ""; if (model. orderno. length = 16) {orderno = model. orderno. substring (0, 4) + model. orderno. substring (9, 7);} datetime dt = convert. todatetime ("1900-01-01"); If (model. transdate. length = 8) {datetime. tryparse (model. transdate. substring (0, 4) + "-" + model. transdate. substring (4, 2) + "-" + model. transdate. substring (6, 2), Out DT);} decimal amount = 0; If (model. amount. length = 12) {amount = parsedecimal (model. amount)/100;} bool checkvalue = new chinapay (). getcheck (model. merid, model. orderno, model. amount, model. currencycode, model. transdate, model. transtype, model. status, model. checkvalue); return New chinapaymodel2 () {merid = model. merid, orderno = orderno, transdate = DT, amount = amount, currencycode = parseint (model. currencycode), transtype = model. transtype, status = parseint (model. status) = 1001, checkvalue = checkvalue, Region ID = model. region ID, priv1 = model. priv1 };}
// The following three methods are used to process the request:
/// <Summary> /// request to convert to int type /// </Summary> /// <Param name = "queryname"> </param> /// <Param name = "defaultvalue"> </param> // <returns> </returns> Public static decimal requestdecimal (string queryname, decimal defaultvalue) {decimal value = defaultvalue; If (! String. isnullorempty (httpcontext. current. request [queryname]) {decimal. tryparse (httputility. htmldecode (httpcontext. current. request [queryname]. tostring (). trim (), out value);} return value ;} /// <summary> /// request to convert string /// </Summary> /// <Param name = "queryname"> </param> /// <Param name = "defaultvalue"> </param> // <returns> </returns> Public static string requeststring (string queryname, string DEFA Ultvalue) {string value = defaultvalue; If (! String. isnullorempty (httpcontext. current. request [queryname]) {value = httputility. htmldecode (httpcontext. current. request [queryname]. tostring (). trim ();} return value ;} /// <summary> /// request to convert to decimal type /// </Summary> /// <Param name = "queryname"> </param> /// <Param name = "defaultvalue"> </param> // <returns> </returns> Public static decimal requestdecimal (string queryname, decimal defaultvalue) {deci Mal value = defaultvalue; If (! String. isnullorempty (httpcontext. current. request [queryname]) {decimal. tryparse (httputility. htmldecode (httpcontext. current. request [queryname]. tostring (). trim (), out value);} return value ;}
   protected void Page_Load(object sender, EventArgs e)    {
Chinapaymodel1 model1 = commonhelper. chinapaymodel1 (); chinapaymodel2 model2 = commonhelper. chinapaymodel2 (); # view received data during region test string logmsg = jsonconvert. serializeobject (New chinapaymode () {requestform = request. form. tostring (), requestquerystring = request. querystring. tostring (), model1 = model1, model2 = model2}); // omitted # endregion if (model2.checkvalue & model2.status ){}
}

  

Simple record: UnionPay online payment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.