AllPay(欧付宝)支付接口集成

AllPay,http://www.allpay.com.tw/,欧付宝是台湾知名的第三方支付公司,拥有丰富的支付模式(支持和支付宝、财付通),只需要一次对接,各种支付模式均可使用。

接口编写SDK:http://www.allpay.com.tw/Service/API_Help?Anchor=AnchorDoc

官方提供了比较完整的SDK,里面有丰富的代码,这里只讲需要注意的地方:

1.欧付宝提供若干支付模式,可以再接口中指定(不给用户选择的机会);也可以在接口中设置ALL,等到付款的时候让用户选择

2.确定支付模式后,便可以生成订单,然后等待支付

3.支付结果的确认是Server静默方式通知的,非URL跳转的方式,这点类似PayPal的Classic模式

4.提供沙箱模式http://vendor-stage.allpay.com.tw,用户名StageTest,密码Test1234,在沙箱中可以任意下订单,模拟支付等等

5.正式模式的后台http://vendor.allpay.com.tw同样支持模拟付款的方式

6.支付结果接收service,注意需要验证来源,也就是CheckMacValue,验证方法后面给出

7.如果接口中不指定支付模式,oPayment.Send.ChoosePayment = PaymentMethod.ALL; 即使客户不选择支付宝支付,那么也需要将支付宝的支付选项填入

oPayment.SendExtend.PhoneNo = "";
oPayment.SendExtend.Email = "";
oPayment.SendExtend.UserName = "";

因为这三项是必填字段,注意是SendExtend。

8.ReturnURL 為Server 端的回應,歐付寶Server會直接將付款結果送到您指定的ReturnURL

   OrderResultURL 則為 Client 端的回應,我們會將使用者的畫面轉導到您指定的OrderResultURL

   ClientBackURL 主要的功能,是在交易完成的頁面中,若廠商端有設定網址,則在該頁面上會出現「返回商店」的按鈕,當消費者點選後,會導到所設定的網址。

9.MerchantTradeNo测试的时候如果两笔都是一样的,会导致第二次创建订单失败,这个要注意,每次最好用随机数


下面是支付界面,传入订单编号,通过调用AllPay接口,会默认跳转到AllPay去支付
        public ActionResult PaymentByAllPay(Guid BillCode)
        {
            List<string> enErrors = new List<string>();
            string szHtml = String.Empty;
            try
            {
                using (AllInOne oPayment = new AllInOne())
                {
                    /* 服務參數 */
                    oPayment.ServiceMethod = AllPay.Payment.Integration.HttpMethod.HttpPOST;
                    //oPayment.ServiceURL = "http://payment-stage.allpay.com.tw/Cashier/AioCheckOut";
                    oPayment.ServiceURL = "https://payment.allpay.com.tw/Cashier/AioCheckOut";
                    oPayment.HashKey = System.Configuration.ConfigurationManager.AppSettings["APHashKey"];
                    oPayment.HashIV = System.Configuration.ConfigurationManager.AppSettings["APHashIV"];
                    oPayment.MerchantID = System.Configuration.ConfigurationManager.AppSettings["MerchantID"];

                    IList<COM_Bill> list = WMSFactory.COM_Bill.FindByCondition("");
                    if (list == null || list.Count() != 1)
                    {
                        throw new Exception("Error:BillCode Is Wrong!");
                    }
                    COM_Bill bill = list.First();
                    IList<COM_BillSub> billSub = WMSFactory.COM_BillSub.FindByCondition("BillCode='" + BillCode.ToString() + "'");

                    /* 基本參數 */
                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Order/";
                    //以后台服务通知的形式发送到该URL
                    oPayment.Send.ReturnURL = baseURI + "AllPayResult/"+BillCode.ToString().Replace("-","_");//完成付款,注意这里有严格的url限制,不能传递-
                    oPayment.Send.ClientBackURL = baseURI;


                    oPayment.Send.MerchantTradeNo = bill.Id.ToString()+DateTime.Parse(bill.CreateTime).ToString("yyyyMMddHHmmss") + new Random().Next(0, 9999).ToString();
                    oPayment.Send.MerchantTradeDate = DateTime.Now;
                    oPayment.Send.TotalAmount = (int)(bill.TotalAmount + bill.ShippingFee - bill.VoucherMoney);
                    oPayment.Send.TradeDesc = "物流费:" + bill.ShippingFee.ToString();
                    oPayment.Send.ChoosePayment = PaymentMethod.ALL;
                    oPayment.Send.ChooseSubPayment = PaymentMethodItem.None;
                    oPayment.Send.NeedExtraPaidInfo = ExtraPaymentInfo.No;
                    oPayment.Send.DeviceSource = DeviceType.PC;
                    //例(排除支付寶與財富通): Alipay#Tenpay
                    oPayment.Send.IgnorePayment = ""; 
                    

                    // 加入選購商品資料。
                    foreach (COM_BillSub sb in billSub)
                    {
                        AllPay.Payment.Integration.Item item = new AllPay.Payment.Integration.Item();
                        item.Name = sb.SkcName + ":" + sb.Skc.ToString() + "," + sb.ColorName + "," + sb.SizeName;
                        item.Currency = "新台幣";
                        item.Price = sb.Price;
                        item.Quantity = sb.Num;
                        item.URL = "";
                        oPayment.Send.Items.Add(item);

                        //oPayment.SendExtend.AlipayItemCounts += sb.Num.ToString() + "#";
                        //oPayment.SendExtend.AlipayItemName += sb.SkcName + "#";
                        //oPayment.SendExtend.AlipayItemPrice += sb.Price.ToString() + "#";
                    }//物流费用
                    AllPay.Payment.Integration.Item shippingfeeitm = new AllPay.Payment.Integration.Item();
                    shippingfeeitm.Name = "Shipping";
                    shippingfeeitm.Currency = "新台幣";
                    shippingfeeitm.Price = bill.ShippingFee;
                    shippingfeeitm.Quantity = 1;
                    oPayment.Send.Items.Add(shippingfeeitm);

                    //oPayment.SendExtend.AlipayItemCounts += "1";
                    //oPayment.SendExtend.AlipayItemName += "Shipping";
                    //oPayment.SendExtend.AlipayItemPrice += bill.ShippingFee.ToString();
                    
                    //获取物流信息
                    COM_Address address = WMSFactory.COM_Address.FindById(bill.ShippingId.ToString());
                    oPayment.SendExtend.PhoneNo = address.TelPhone;
                    oPayment.SendExtend.Email = member.Email;
                    oPayment.SendExtend.UserName = address.LastName + address.FirstName;

                    /* 產生訂單 */
                    enErrors.AddRange(oPayment.CheckOut());
                    /* 產生產生訂單 Html Code 的方法 */
                    enErrors.AddRange(oPayment.CheckOutString(ref szHtml));

                    Session.Add(BillCode.ToString(), oPayment.Send.MerchantTradeNo);
                }
            }
            catch (Exception ex)
            {
                // 例外錯誤處理。
                enErrors.Add(ex.Message);
            }
            finally
            {
                if (enErrors.Count() > 0)
                    throw new Exception(String.Join("<br />", enErrors));
            }
            //扣库存
            DataTable dt = WMSFactory.COM_BillSub.UpdateStore(BillCode, 2);
            if (dt != null && dt.Rows.Count > 0 && dt.Rows[0][0].ToString() != "0")
            {
                return RedirectToAction("PayResult", new { id = 3, msg = dt.Rows[0][0].ToString() });
            }
            ViewBag.AllPayRedirect = szHtml;
            return View();
        }

 

支付确认接口实现:

public void AllPayResult(string id)
        {
            List<string> enErrors = new List<string>();
            Hashtable htFeedback = null;
            id = id.Replace("_", "-");
            log.Debug("进入allpayresult,id=" + id);
            try
            {
                string szHashKey = System.Configuration.ConfigurationManager.AppSettings["APHashKey"];
                string szHashIV = System.Configuration.ConfigurationManager.AppSettings["APHashIV"];
                
                // 取回所有資料 
                if (enErrors.Count() == 0)
                {         /* 支付後的回傳的基本參數 */
                    string szMerchantID = Request.Form["MerchantID"];
                    string szMerchantTradeNo = Request.Form["MerchantTradeNo"];
                    string szPaymentDate =Request.Form["PaymentDate"];
                    string szPaymentType =  Request.Form["PaymentType"];
                    string szPaymentTypeChargeFee =  Request.Form["PaymentTypeChargeFee"];
                    string szRtnCode = Request.Form["RtnCode"];
                    string szRtnMsg = Request.Form["RtnMsg"];
                    string szSimulatePaid = Request.Form["SimulatePaid"];
                    string szTradeAmt =Request.Form["TradeAmt"];
                    string szTradeDate =Request.Form["TradeDate"];
                    string szTradeNo = Request.Form["TradeNo"];
                    string szCheckMacValue = Request.Form["CheckMacValue"];
                     

                    //一般回傳參數CMV(若需要其他檢查請自行增加)
                    Dictionary<string, string> Parameters = new Dictionary<string, string>();
                    Parameters.Add("MerchantID", szMerchantID);
                    Parameters.Add("MerchantTradeNo", szMerchantTradeNo);
                    Parameters.Add("RtnCode", szRtnCode);
                    Parameters.Add("RtnMsg", szRtnMsg);
                    Parameters.Add("TradeNo", szTradeNo);
                    Parameters.Add("TradeAmt", szTradeAmt);
                    Parameters.Add("PaymentDate", szPaymentDate);
                    Parameters.Add("PaymentType", szPaymentType);
                    Parameters.Add("PaymentTypeChargeFee", szPaymentTypeChargeFee);
                    Parameters.Add("TradeDate", szTradeDate);
                    Parameters.Add("SimulatePaid", szSimulatePaid);

                    string ParameterString = string.Join("&", Parameters.OrderBy(d => d.Key).Select(p => p.Key + "=" + p.Value));

                    string str = "HashKey=" + szHashKey + "&" + ParameterString + "&HashIV=" + szHashIV;

                    log.Debug("接收到AllPay返回值=" + str);

                    string urlEncodeStrPost = HttpUtility.UrlEncode(str);
                    //url大寫轉小寫
                    string lowerUrlEncodeStrPost = urlEncodeStrPost.ToLower();
                    //檢查碼要使用MD5加密
                    MD5 md5Hasher = MD5.Create();
                    byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(lowerUrlEncodeStrPost));
                    StringBuilder sBuilder = new StringBuilder();
                    for (int i = 0; i < data.Length; i++)
                    {
                        sBuilder.Append(data[i].ToString("X2"));
                    }
                    string sCheckMacValue = sBuilder.ToString();
                    log.Debug("mine:" + sCheckMacValue + ";param:" + szCheckMacValue);
                    if (szRtnCode == "1" && szSimulatePaid == "0" && sCheckMacValue == szCheckMacValue)
                    {
                        //更新订单状态
                    }
                }
            }
            catch (Exception ex)
            {     // 例外錯誤處理。 
                enErrors.Add(ex.Message);
            }
            finally
            {
                this.Response.Clear();     // 回覆成功訊息。 
                if (enErrors.Count() == 0)
                    this.Response.Write("1|OK");     // 回覆錯誤訊息。 
                else
                    this.Response.Write(String.Format("0|{0}", String.Join("\\r\\n", enErrors)));
                this.Response.Flush();
                this.Response.End();
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值