wxpayapi
using System; namespace EPayInterfaceApp { public class EPayInterfaceApp { /** * 提交被掃支付API * 收銀員使用掃碼設備讀取微信用戶刷卡授權碼以后,二維碼或條碼信息傳送至商戶收銀台, * 由商戶收銀台或者商戶后台調用該接口發起支付。 * @param WxPayData inputObj 提交給被掃支付API的參數 * @param int timeOut 超時時間 * @throws WxPayException * @return 成功時返回調用結果,其他拋異常 */ public static WxPayData Micropay(WxPayData inputObj) { int timeOut = 10; string url = "https://api.mch.weixin.qq.com/pay/micropay"; //檢測必填參數 if (!inputObj.IsSet("body")) { throw new WxPayException("提交被掃支付API接口中,缺少必填參數body!"); } else if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("提交被掃支付API接口中,缺少必填參數out_trade_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("提交被掃支付API接口中,缺少必填參數total_fee!"); } else if (!inputObj.IsSet("auth_code")) { throw new WxPayException("提交被掃支付API接口中,缺少必填參數auth_code!"); } inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//終端ip inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//隨機字符串 inputObj.SetValue("sign", inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now;//請求開始時間 Log.Debug("EPayInterfaceApp", "MicroPay request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//調用HTTP通信接口以提交數據到API Log.Debug("EPayInterfaceApp", "MicroPay response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//獲得接口耗時 //將xml格式的結果轉換為對象以返回 WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 查詢訂單 * @param WxPayData inputObj 提交給查詢訂單API的參數 * @param int timeOut 超時時間 * @throws WxPayException * @return 成功時返回訂單查詢結果,其他拋異常 */ public static WxPayData OrderQuery(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/pay/orderquery"; //檢測必填參數 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("訂單查詢接口中,out_trade_no、transaction_id至少填一個!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str", EPayInterfaceApp.GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign", inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; Log.Debug("EPayInterfaceApp", "OrderQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//調用HTTP通信接口提交數據 Log.Debug("EPayInterfaceApp", "OrderQuery response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//獲得接口耗時 //將xml格式的數據轉化為對象以返回 WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 撤銷訂單API接口 * @param WxPayData inputObj 提交給撤銷訂單API接口的參數,out_trade_no和transaction_id必填一個 * @param int timeOut 接口超時時間 * @throws WxPayException * @return 成功時返回API調用結果,其他拋異常 */ public static WxPayData Reverse(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; //檢測必填參數 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("撤銷訂單API接口中,參數out_trade_no和transaction_id必須填寫一個!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str", GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign", inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now;//請求開始時間 Log.Debug("EPayInterfaceApp", "Reverse request : " + xml); string response = HttpService.Post(xml, url, true, timeOut); Log.Debug("EPayInterfaceApp", "Reverse response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 申請退款 * @param WxPayData inputObj 提交給申請退款API的參數 * @param int timeOut 超時時間 * @throws WxPayException * @return 成功時返回接口調用結果,其他拋異常 */ public static WxPayData Refund(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //檢測必填參數 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("退款申請接口中,out_trade_no、transaction_id至少填一個!"); } else if (!inputObj.IsSet("out_refund_no")) { throw new WxPayException("退款申請接口中,缺少必填參數out_refund_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("退款申請接口中,缺少必填參數total_fee!"); } else if (!inputObj.IsSet("refund_fee")) { throw new WxPayException("退款申請接口中,缺少必填參數refund_fee!"); } else if (!inputObj.IsSet("op_user_id")) { throw new WxPayException("退款申請接口中,缺少必填參數op_user_id!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//隨機字符串 inputObj.SetValue("sign", inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; Log.Debug("EPayInterfaceApp", "Refund request : " + xml); string response = HttpService.Post(xml, url, true, timeOut);//調用HTTP通信接口提交數據到API Log.Debug("EPayInterfaceApp", "Refund response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//獲得接口耗時 //將xml格式的結果轉換為對象以返回 WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 查詢退款 * 提交退款申請后,通過該接口查詢退款狀態。退款有一定延時, * 用零錢支付的退款20分鍾內到賬,銀行卡支付的退款3個工作日后重新查詢退款狀態。 * out_refund_no、out_trade_no、transaction_id、refund_id四個參數必填一個 * @param WxPayData inputObj 提交給查詢退款API的參數 * @param int timeOut 接口超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */ public static WxPayData RefundQuery(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/pay/refundquery"; //檢測必填參數 if(!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id")) { throw new WxPayException("退款查詢接口中,out_refund_no、out_trade_no、transaction_id、refund_id四個參數必填一個!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str",GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign",inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now;//請求開始時間 Log.Debug("EPayInterfaceApp", "RefundQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//調用HTTP通信接口以提交數據到API Log.Debug("EPayInterfaceApp", "RefundQuery response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end-start).TotalMilliseconds);//獲得接口耗時 //將xml格式的結果轉換為對象以返回 WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * 下載對賬單 * @param WxPayData inputObj 提交給下載對賬單API的參數 * @param int timeOut 接口超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */ public static WxPayData DownloadBill(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/pay/downloadbill"; //檢測必填參數 if (!inputObj.IsSet("bill_date")) { throw new WxPayException("對賬單接口中,缺少必填參數bill_date!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str", GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign", inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); Log.Debug("EPayInterfaceApp", "DownloadBill request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//調用HTTP通信接口以提交數據到API Log.Debug("EPayInterfaceApp", "DownloadBill result : " + response); WxPayData result = new WxPayData(); //若接口調用失敗會返回xml格式的結果 if (response.Substring(0, 5) == "<xml>") { result.FromXml(response); } //接口調用成功則返回非xml格式的數據 else result.SetValue("result", response); return result; } /** * * 轉換短鏈接 * 該接口主要用於掃碼原生支付模式一中的二維碼鏈接轉成短鏈接(weixin://wxpay/s/XXXXXX), * 減小二維碼數據量,提升掃描速度和精確度。 * @param WxPayData inputObj 提交給轉換短連接API的參數 * @param int timeOut 接口超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */ public static WxPayData ShortUrl(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/tools/shorturl"; //檢測必填參數 if(!inputObj.IsSet("long_url")) { throw new WxPayException("需要轉換的URL,簽名用原串,傳輸需URL encode!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str",GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign",inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now;//請求開始時間 Log.Debug("EPayInterfaceApp", "ShortUrl request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("EPayInterfaceApp", "ShortUrl response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 統一下單 * @param WxPaydata inputObj 提交給統一下單API的參數 * @param int timeOut 超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */ public static WxPayData UnifiedOrder(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //檢測必填參數 if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("缺少統一支付接口必填參數out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WxPayException("缺少統一支付接口必填參數body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("缺少統一支付接口必填參數total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WxPayException("缺少統一支付接口必填參數trade_type!"); } //關聯參數 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WxPayException("統一支付接口中,缺少必填參數openid!trade_type為JSAPI時,openid為必填參數!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WxPayException("統一支付接口中,缺少必填參數product_id!trade_type為JSAPI時,product_id為必填參數!"); } //異步通知url未設置,則使用配置文件中的url if (!inputObj.IsSet("notify_url")) { inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//異步通知url } inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//終端ip inputObj.SetValue("nonce_str", GenerateNonceStr());//隨機字符串 //簽名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); DateTime start = DateTime.Now; Log.Debug("EPayInterfaceApp", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("EPayInterfaceApp", "UnfiedOrder response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 關閉訂單 * @param WxPayData inputObj 提交給關閉訂單API的參數 * @param int timeOut 接口超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */ public static WxPayData CloseOrder(WxPayData inputObj) { int timeOut = 6; string url = "https://api.mch.weixin.qq.com/pay/closeorder"; //檢測必填參數 if(!inputObj.IsSet("out_trade_no")) { throw new WxPayException("關閉訂單接口中,out_trade_no必填!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商戶號 inputObj.SetValue("nonce_str",GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign",inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now;//請求開始時間 string response = HttpService.Post(xml, url, false, timeOut); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//測速上報 return result; } /** * * 測速上報 * @param string interface_url 接口URL * @param int timeCost 接口耗時 * @param WxPayData inputObj參數數組 */ private static void ReportCostTime(string interface_url, int timeCost, WxPayData inputObj) { //如果僅失敗上報 if(WxPayConfig.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" && inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS") { return; } //上報邏輯 WxPayData data = new WxPayData(); data.SetValue("interface_url",interface_url); data.SetValue("execute_time_",timeCost); //返回狀態碼 if(inputObj.IsSet("return_code")) { data.SetValue("return_code",inputObj.GetValue("return_code")); } //返回信息 if(inputObj.IsSet("return_msg")) { data.SetValue("return_msg",inputObj.GetValue("return_msg")); } //業務結果 if(inputObj.IsSet("result_code")) { data.SetValue("result_code",inputObj.GetValue("result_code")); } //錯誤代碼 if(inputObj.IsSet("err_code")) { data.SetValue("err_code",inputObj.GetValue("err_code")); } //錯誤代碼描述 if(inputObj.IsSet("err_code_des")) { data.SetValue("err_code_des",inputObj.GetValue("err_code_des")); } //商戶訂單號 if(inputObj.IsSet("out_trade_no")) { data.SetValue("out_trade_no",inputObj.GetValue("out_trade_no")); } //設備號 if(inputObj.IsSet("device_info")) { data.SetValue("device_info",inputObj.GetValue("device_info")); } try { Report(data); } catch (WxPayException ex) { //不做任何處理 } } /** * * 測速上報接口實現 * @param WxPayData inputObj 提交給測速上報接口的參數 * @param int timeOut 測速上報接口超時時間 * @throws WxPayException * @return 成功時返回測速上報接口返回的結果,其他拋異常 */ public static WxPayData Report(WxPayData inputObj) { int timeOut = 1; string url = "https://api.mch.weixin.qq.com/payitil/report"; //檢測必填參數 if(!inputObj.IsSet("interface_url")) { throw new WxPayException("接口URL,缺少必填參數interface_url!"); } if(!inputObj.IsSet("return_code")) { throw new WxPayException("返回狀態碼,缺少必填參數return_code!"); } if(!inputObj.IsSet("result_code")) { throw new WxPayException("業務結果,缺少必填參數result_code!"); } if(!inputObj.IsSet("user_ip")) { throw new WxPayException("訪問接口IP,缺少必填參數user_ip!"); } if(!inputObj.IsSet("execute_time_")) { throw new WxPayException("接口耗時,缺少必填參數execute_time_!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商戶號 inputObj.SetValue("user_ip",WxPayConfig.IP);//終端ip inputObj.SetValue("time",DateTime.Now.ToString("yyyyMMddHHmmss"));//商戶上報時間 inputObj.SetValue("nonce_str",GenerateNonceStr());//隨機字符串 inputObj.SetValue("sign",inputObj.MakeSign());//簽名 string xml = inputObj.ToXml(); Log.Info("EPayInterfaceApp", "Report request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Info("EPayInterfaceApp", "Report response : " + response); WxPayData result = new WxPayData(); result.FromXml(response); return result; } /** * 根據當前系統時間加隨機序列來生成訂單號 * @return 訂單號 */ public static string GenerateOutTradeNo() { Random ran = new Random(); return string.Format("{0}{1}{2}", WxPayConfig.MCHID, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999)); } /** * 生成時間戳,標准北京時間,時區為東八區,自1970年1月1日 0點0分0秒以來的秒數 * @return 時間戳 */ public static string GenerateTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } /** * 生成隨機串,隨機串包含字母或數字 * @return 隨機串 */ public static string GenerateNonceStr() { return Guid.NewGuid().ToString().Replace("-", ""); } } }
Alipayapi
#region 支付寶接口 /* {"alipay_trade_pay_response":{"code":"40004","msg":"Business Failed","sub_code":"ACQ.SELLER_NOT_EXIST", "sub_msg":"支付失敗,本商戶賬號異常,請聯系管理員處理,建議顧客使用其他方式付款。[SELLER_NOT_EXIST]", "buyer_pay_amount":"0.00","invoice_amount":"0.00", "out_trade_no":"735a3265eed6b609", "point_amount":"0.00","receipt_amount":"0.00"}, "sign":"ArSRYRy6SlLLPQWSRHI596b9h/cv+AKK92oWCB3H4bwPzLr9LZ78j5kUVVzvdd2nME1V8h5M2P0SRoy/kujGB/V8N9oJCgaaX856xgZIevkNhXpILMdxaNTc3qgcoPzAduZ+aKx2sktrwA2KbtbdK2jjE8eeDqTcEgng2hwKEbA="} {"alipay_trade_pay_response":{"code":"10000","msg":"Success", "buyer_logon_id":"710***@qq.com","buyer_pay_amount":"0.01","buyer_user_id":"2088002008293033", "fund_bill_list":[{"amount":"0.01","fund_channel":"ALIPAYACCOUNT"}], "gmt_payment":"2016-09-29 10:51:03","invoice_amount":"0.01", "open_id":"20881068707802584019802220315503", "out_trade_no":"5e2a9a41f89ec6d9", "point_amount":"0.00","receipt_amount":"0.01", "total_amount":"0.01","trade_no":"2016092921001004030263332463"}, "sign":"j2SLzhk+CfUuSj5nW3fnumiEgy3HJhKVyWzGfmFJVp6HkkhcVNjhbKod6jDbE2hBOpvfT7bC1nPUFQty+gIyc2A7IAc6R8uEXEVVTeTM9DnUuU+gC+w+QOOu8O7i443zIl8Pm+G0a9NX5bdyJRDGLNiTKbFXcHrJYKE2sJHQL7o="} */ /// <summary> /// 發起付款請求 /// </summary> /// <param name="barcode"></param> private void execAliPayAction(string barcode) { string payResultJson = string.Empty; AlipayTradePayContentBuilder builder = BuildPayContent(barcode); string out_trade_no = builder.out_trade_no; AlipayF2FPayResult payResult = serviceClient.tradePay(builder); switch (payResult.Status) { case ResultEnum.SUCCESS: payResultJson = DoSuccessProcess(payResult); break; case ResultEnum.FAILED: payResultJson = DoFailedProcess(payResult); break; case ResultEnum.UNKNOWN: payResultJson = @"{""code"":""-99"",""msg"":""網絡異常,請檢查網絡配置后,更換外部訂單號重試""}"; break; } JsonData resultJsonData = JsonMapper.ToObject(payResultJson); JsonData resultJson = resultJsonData["alipay_trade_pay_response"]; string code = (string)resultJson["code"]; string msg = (string)resultJson["msg"]; if (code == "10000" && msg == "Success") { iniHelper.IniWriteValue("interfaceResult", "code", "0"); iniHelper.IniWriteValue("interfaceResult", "message", "操作成功!"); iniHelper.IniWriteValue("interfaceResult", "order_no", (string)resultJson["out_trade_no"]); iniHelper.IniWriteValue("interfaceResult", "open_id", (string)resultJson["open_id"]); iniHelper.IniWriteValue("interfaceResult", "transact_id", (string)resultJson["trade_no"]); iniHelper.IniWriteValue("interfaceResult", "buyer_info", (string)resultJson["buyer_user_id"]+","+ (string)resultJson["buyer_logon_id"]); if (Program.erpHandle != IntPtr.Zero) Utility.SendMessage(Program.erpHandle, (uint)Program.NOTIFY_MESSAGE, 0, 0); isRuning = false; Application.ExitThread(); Application.Exit(); } else { iniHelper.IniWriteValue("interfaceResult", "code", "-"+ (string)resultJson["code"]); iniHelper.IniWriteValue("interfaceResult", "message", (string)resultJson["sub_msg"]); iniHelper.IniWriteValue("interfaceResult", "order_no", (string)resultJson["out_trade_no"]); if (Program.erpHandle != IntPtr.Zero) Utility.SendMessage(Program.erpHandle, (uint)Program.NOTIFY_MESSAGE, 0, 0); isRuning = false; Application.ExitThread(); Application.Exit(); } } private AlipayTradePayContentBuilder BuildPayContent(string barcode) { //線上聯調時,請輸入真實的外部訂單號。 string out_trade_no = GuidTo16String(); //掃碼槍掃描到的用戶手機錢包中的付款條碼 AlipayTradePayContentBuilder builder = new AlipayTradePayContentBuilder(); builder.out_trade_no = out_trade_no; builder.scene = "bar_code"; builder.auth_code = barcode; builder.total_amount = paymentRow.ItemDataRow[2].ToString(); builder.discountable_amount = paymentRow.ItemDataRow[2].ToString(); builder.undiscountable_amount = "0"; builder.operator_id = "alipay_mayun"; builder.subject = string.Format("{0}消費", StoreName); builder.timeout_express = "2m"; builder.body = "訂單描述"; builder.store_id = string.Format("{0}消費", StoreName); //很重要的參數,可以用作之后的營銷 builder.seller_id = Config.pid; //可以是具體的收款賬號。 //傳入商品信息詳情 List<GoodsInfo> gList = new List<GoodsInfo>(); GoodsInfo goods = new GoodsInfo(); goods.goods_id = "goods id"; goods.goods_name = builder.subject; goods.price = builder.total_amount; goods.quantity = "1"; gList.Add(goods); builder.goods_detail = gList; //擴展參數 //系統商接入可以填此參數用作返佣 //ExtendParams exParam = new ExtendParams(); //exParam.sysServiceProviderId = Config.pid; //builder.extendParams = exParam; return builder; } /// <summary> /// 顧客主動掃描二維碼構建 /// </summary> /// <returns></returns> private AlipayTradePrecreateContentBuilder BuildPrecreateContent() { //線上聯調時,請輸入真實的外部訂單號。 string out_trade_no = GuidTo16String(); AlipayTradePrecreateContentBuilder builder = new AlipayTradePrecreateContentBuilder(); builder.out_trade_no = out_trade_no; builder.total_amount = paymentRow.ItemDataRow[2].ToString(); builder.undiscountable_amount = "0"; builder.operator_id = "alipay_mayun"; builder.subject = "掃碼支付"; builder.time_expire = System.DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss"); ; builder.body = "訂單描述"; builder.store_id = string.Format("{0}消費", StoreName); //很重要的參數,可以用作之后的營銷 builder.seller_id = Config.pid; //可以是具體的收款賬號。 //傳入商品信息詳情 List<GoodsInfo> gList = new List<GoodsInfo>(); GoodsInfo goods = new GoodsInfo(); goods.goods_id = "goods id"; goods.goods_name = "goods name"; goods.price = builder.total_amount; goods.quantity = "1"; gList.Add(goods); builder.goods_detail = gList; //擴展參數 //系統商接入可以填此參數用作返佣 //ExtendParams exParam = new ExtendParams(); //exParam.sysServiceProviderId = "20880000000000"; //builder.extendParams = exParam; return builder; } /// <summary> /// 請添加支付成功后的處理 /// </summary> private string DoSuccessProcess(AlipayF2FPayResult payResult) { Com.DataCool.DotNetExpand.LogHelper.Info("支付成功"); result = payResult.response.Body; return result; } private string DoFailedProcess(AlipayF2FPayResult payResult) { //請添加支付失敗后的處理 Com.DataCool.DotNetExpand.LogHelper.Error("支付失敗"); result = payResult.response.Body; return result; } /// <summary> /// 根據GUID獲取16位的唯一字符串 /// </summary> /// <param name=\"guid\"></param> /// <returns></returns> public static string GuidTo16String() { long i = 1; foreach (byte b in Guid.NewGuid().ToByteArray()) i *= ((int)b + 1); return string.Format("{0:x}", i - DateTime.Now.Ticks); } /// <summary> ///退款請求消息報文構建 /// </summary> /// <param name="trade_no"></param> /// <param name="request_no"></param> /// <returns></returns> private AlipayTradeRefundContentBuilder BuildContent(string trade_no, string request_no) { AlipayTradeRefundContentBuilder builder = new AlipayTradeRefundContentBuilder(); //商戶訂單號 builder.out_trade_no = trade_no; //交易號——退款請求單號保持唯一性。 builder.out_request_no = request_no; //退款金額 builder.refund_amount = paymentRow.ItemDataRow[2].ToString(); builder.refund_reason = "銷售退貨"; return builder; } #endregion