银联支付

对银联支付的二次封装,支持pc端

类图

 

pay(pc):

  1.  
    var title = "银联支付";
  2.  
    var orderNo = ""; //订单号
  3.  
    var tradeMoney = 0.00;
  4.  
    var notifyUrl = "host/NotifyCallback.aspx";
  5.  
    var returnUrl = "host/ReturnUrl.aspx";
  6.  
    var key = ""; //账户的key
  7.  
    var partner = ""; //账户的partnerid
  8.  
    var chairtyName = ""; //账户显示名称
  9.  
    var unionPay = new UnionPay.Submit(key, partner, chairtyName));
  10.  
    unionPay.Pay(title, orderNo, tradeMoney, notifyUrl, returnUrl);

 

return(pc):

  1.  
    public partial class NotifyUrl : UnionPay.NotifyPage
  2.  
    {
  3.  
    protected override void OnNotifyConfirm()
  4.  
    {
  5.  
    //todo:业务逻辑处理
  6.  
    }
  7.  
    }

 

Submit.cs

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.IO;
  4.  
    using System.Web;
  5.  
    using com.unionpay.upop.sdk;
  6.  
     
  7.  
    namespace UnionPay
  8.  
    {
  9.  
    public class Submit
  10.  
    {
  11.  
    /// <summary>
  12.  
    /// 实例化,默认UinonPay.config
  13.  
    /// </summary>
  14.  
    public Submit()
  15.  
    {
  16.  
    UPOPSrv.LoadConf(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UinonPay.config"));
  17.  
    }
  18.  
     
  19.  
    /// <summary>
  20.  
    /// 实例化
  21.  
    /// </summary>
  22.  
    /// <param name="configPath">配置文件全路径</param>
  23.  
    public Submit(string configPath)
  24.  
    {
  25.  
    UPOPSrv.LoadConf(configPath);
  26.  
    }
  27.  
     
  28.  
    /// <summary>
  29.  
    /// 实例化
  30.  
    /// </summary>
  31.  
    /// <param name="key">key</param>
  32.  
    /// <param name="partner">partnetid</param>
  33.  
    /// <param name="chairtyName">账户名称</param>
  34.  
    public Submit(string key, string partner, string chairtyName)
  35.  
    {
  36.  
    var stream = new Config().SetConfig(key, partner, chairtyName);
  37.  
    UPOPSrv.LoadConf(stream);
  38.  
    }
  39.  
     
  40.  
    public void Pay(string title, string orderNo, decimal tradeMoney, string notifyUrl, string returnUrl)
  41.  
    {
  42.  
    var param = new Dictionary<string, string>
  43.  
    {
  44.  
    {"transType", UPOPSrv.TransType.CONSUME}, //交易类型,前台只支持CONSUME 和PRE_AUTH
  45.  
    {"commodityUrl", ""}, //商品URL
  46.  
    {"commodityName", title}, //商品名称
  47.  
    {"commodityUnitPrice", "1"}, //商品单价,分为单位
  48.  
    {"commodityQuantity", "1"}, //商品数量
  49.  
    {"orderNumber", orderNo}, //订单号
  50.  
    {"orderAmount", (tradeMoney * 100).ToString().Split('.')[0]}, //交易金额
  51.  
    {"orderCurrency", UPOPSrv.CURRENCY_CNY}, //币种
  52.  
    {"orderTime", DateTime.Now.ToString("yyyyMMddHHmmss")}, //交易时间
  53.  
    {"customerIp", HttpContext.Current.Request.UserHostAddress}, //用户IP
  54.  
    {"frontEndUrl", notifyUrl}, //前台回调URL
  55.  
    {"backEndUrl", returnUrl} //后台回调URL(前台请求时可为空)
  56.  
    };
  57.  
    var srv = new FrontPaySrv(param);
  58.  
    HttpContext.Current.Response.Write(srv.CreateHtml());
  59.  
    }
  60.  
    }
  61.  
    }

 

Config.cs

  1.  
    using System.IO;
  2.  
    using System.Xml;
  3.  
     
  4.  
    namespace UnionPay
  5.  
    {
  6.  
    public class Config
  7.  
    {
  8.  
    private static XmlDocument _xmlDoc;
  9.  
     
  10.  
    /// <summary>
  11.  
    /// xml模板
  12.  
    /// </summary>
  13.  
    private static XmlDocument XmlDoc
  14.  
    {
  15.  
    get
  16.  
    {
  17.  
    if (_xmlDoc == null)
  18.  
    {
  19.  
    _xmlDoc = new XmlDocument();
  20.  
    _xmlDoc.Load("Template.config");
  21.  
    }
  22.  
    return _xmlDoc;
  23.  
    }
  24.  
    }
  25.  
     
  26.  
    public Stream SetConfig(string key, string partner, string chairtyName)
  27.  
    {
  28.  
    var ConfigInf = XmlDoc.SelectSingleNode("ConfigInf");
  29.  
    var securityKey = ConfigInf.SelectSingleNode("securityKey");
  30.  
    securityKey.InnerText = key;
  31.  
     
  32.  
    var payParamsPredef = ConfigInf.SelectSingleNode("payParamsPredef");
  33.  
    var merId = payParamsPredef.SelectSingleNode("merId");
  34.  
    merId.InnerText = partner;
  35.  
     
  36.  
    var merAbbr = payParamsPredef.SelectSingleNode("merAbbr");
  37.  
    merAbbr.InnerText = chairtyName;
  38.  
     
  39.  
    var bytes = System.Text.Encoding.UTF8.GetBytes(XmlDoc.InnerXml);
  40.  
    var stream = new MemoryStream(bytes);
  41.  
     
  42.  
    return stream;
  43.  
    }
  44.  
    }
  45.  
    }

 

Template.config

  1.  
    <?xml version="1.0"?>
  2.  
    <ConfigInf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3.  
    <signMethod>MD5</signMethod>
  4.  
    <securityKey>{$key}</securityKey>
  5.  
    <frontPayURL>https://unionpaysecure.com/api/Pay.action</frontPayURL>
  6.  
    <backPayURL>https://unionpaysecure.com/api/BSPay.action</backPayURL>
  7.  
    <queryURL>https://unionpaysecure.com/api/Query.action</queryURL>
  8.  
    <SSLCertPolicy>Ignore</SSLCertPolicy>
  9.  
    <payParamsPredef>
  10.  
    <version>1.0.0</version>
  11.  
    <charset>UTF-8</charset>
  12.  
    <merId>{$merId}</merId>
  13.  
    <acqCode></acqCode>
  14.  
    <merCode></merCode>
  15.  
    <merAbbr>{$name}</merAbbr>
  16.  
    </payParamsPredef>
  17.  
    <payParams>
  18.  
    <string>version</string>
  19.  
    <string>charset</string>
  20.  
    <string>transType</string>
  21.  
    <string>origQid</string>
  22.  
    <string>merId</string>
  23.  
    <string>merAbbr</string>
  24.  
    <string>acqCode</string>
  25.  
    <string>merCode</string>
  26.  
    <string>commodityUrl</string>
  27.  
    <string>commodityName</string>
  28.  
    <string>commodityUnitPrice</string>
  29.  
    <string>commodityQuantity</string>
  30.  
    <string>commodityDiscount</string>
  31.  
    <string>transferFee</string>
  32.  
    <string>orderNumber</string>
  33.  
    <string>orderAmount</string>
  34.  
    <string>orderCurrency</string>
  35.  
    <string>orderTime</string>
  36.  
    <string>customerIp</string>
  37.  
    <string>customerName</string>
  38.  
    <string>defaultPayType</string>
  39.  
    <string>defaultBankNumber</string>
  40.  
    <string>transTimeout</string>
  41.  
    <string>frontEndUrl</string>
  42.  
    <string>backEndUrl</string>
  43.  
    <string>merReserved</string>
  44.  
    </payParams>
  45.  
    <payParamsNotEmpty>
  46.  
    <string>version</string>
  47.  
    <string>charset</string>
  48.  
    <string>transType</string>
  49.  
    <string>merId</string>
  50.  
    <string>merAbbr</string>
  51.  
    <string>orderNumber</string>
  52.  
    <string>orderAmount</string>
  53.  
    <string>orderCurrency</string>
  54.  
    <string>orderTime</string>
  55.  
    <string>customerIp</string>
  56.  
    <string>frontEndUrl</string>
  57.  
    <string>backEndUrl</string>
  58.  
    </payParamsNotEmpty>
  59.  
    <queryParams>
  60.  
    <string>version</string>
  61.  
    <string>charset</string>
  62.  
    <string>transType</string>
  63.  
    <string>merId</string>
  64.  
    <string>orderNumber</string>
  65.  
    <string>orderTime</string>
  66.  
    <string>merReserved</string>
  67.  
    </queryParams>
  68.  
    <merReservedParams>
  69.  
    <!-- NEW NAME OLD NAME -->
  70.  
    <string>cardNumber</string>
  71.  
    <string>pan</string>
  72.  
    <string>cardPasswd</string>
  73.  
    <string>password</string>
  74.  
    <string>credentialType</string>
  75.  
    <string>idType</string>
  76.  
    <string>cardCvn2</string>
  77.  
    <string>cvn</string>
  78.  
    <string>cardExpire</string>
  79.  
    <string>expire</string>
  80.  
    <string>credentialNumber</string>
  81.  
    <string>idNo</string>
  82.  
    <string>credentialName</string>
  83.  
    <string>name</string>
  84.  
    <string>phoneNumber</string>
  85.  
    <string>mobile</string>
  86.  
    <string>merAbstract</string>
  87.  
    <!-- tdb only -->
  88.  
    <string>orderTimeoutDate</string>
  89.  
    <string>origOrderNumber</string>
  90.  
    <string>origOrderTime</string>
  91.  
    </merReservedParams>
  92.  
    </ConfigInf>

 

NotifyPage.cs

    1.  
      using System;
    2.  
      using System.Globalization;
    3.  
      using System.IO;
    4.  
      using System.Text;
    5.  
       
    6.  
      namespace UnionPay
    7.  
      {
    8.  
      public abstract class NotifyPage : System.Web.UI.Page
    9.  
      {
    10.  
      /// <summary>
    11.  
      /// 商户订单号
    12.  
      /// </summary>
    13.  
      protected string OrderNo { get; private set; }
    14.  
       
    15.  
      /// <summary>
    16.  
      /// 交易金额
    17.  
      /// </summary>
    18.  
      protected decimal TradeMoney { get; private set; }
    19.  
       
    20.  
      /// <summary>
    21.  
      /// 交易状态
    22.  
      /// </summary>
    23.  
      protected string TradeStatus { get; private set; }
    24.  
       
    25.  
      /// <summary>
    26.  
      /// 业务逻辑处理
    27.  
      /// </summary>
    28.  
      protected abstract void OnNotifyConfirm();
    29.  
       
    30.  
      protected void Page_Load(object sender, EventArgs e)
    31.  
      {
    32.  
      if (IsPostBack) return;
    33.  
      OrderNo = Request.Form["orderNumber"];
    34.  
      TradeMoney = Convert.ToDecimal(Request.Form["orderAmount"]);
    35.  
      TradeStatus = Request.Form["respCode"];
    36.  
       
    37.  
      if (TradeStatus == "00")
    38.  
      {
    39.  
      OnNotifyConfirm();
    40.  
      Response.Write("200");
    41.  
      }
    42.  
      Response.End();
    43.  
      }
    44.  
       
    45.  
      /// <summary>
    46.  
      /// 写日志
    47.  
      /// </summary>
    48.  
      protected void WriteLog(string text, string logType)
    49.  
      {
    50.  
      var strPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/log/UnionPay/");
    51.  
      var dateFloderName = DateTime.Now.ToString("yyyyMM");
    52.  
      strPath = string.Format("{0}/{1}", strPath, dateFloderName);
    53.  
      if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
    54.  
      strPath = strPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff", DateTimeFormatInfo.InvariantInfo) + ".txt";
    55.  
      var fs = new StreamWriter(strPath, true, Encoding.Default);
    56.  
      fs.Write(text);
    57.  
      fs.Close();
    58.  
      }
    59.  
      }
    60.  }
posted @ 2019-07-26 18:51  馨馨妙  阅读(250)  评论(0编辑  收藏  举报