NativePay.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. namespace GxPress.Common.WechatPay
  3. {
  4. public class NativePay
  5. {
  6. /**
  7. * 生成直接支付url,支付url有效期为2小时,模式二
  8. * @param productId 商品ID
  9. * @return 模式二URL
  10. */
  11. public string GetPayUrl(string productId,string productInfo,int userId)
  12. {
  13. //Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing...");
  14. try
  15. {
  16. WxPayData data = new WxPayData();
  17. data.SetValue("body", productInfo);//商品描述
  18. data.SetValue("attach", $"{productId}_{userId}");//附加数据
  19. data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());//随机字符串
  20. data.SetValue("total_fee", 1);//总金额
  21. data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
  22. data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
  23. data.SetValue("goods_tag", "jjj");//商品标记
  24. data.SetValue("trade_type", "NATIVE");//交易类型
  25. data.SetValue("product_id", productId);//商品ID
  26. WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
  27. string url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接
  28. //Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url);
  29. return url;
  30. }
  31. catch (System.Exception ex)
  32. {
  33. throw new Common.Exceptions.BusinessException(ex.Message);
  34. }
  35. }
  36. /***
  37. * 申请退款完整业务流程逻辑
  38. * @param transaction_id 微信订单号(优先使用)
  39. * @param out_trade_no 商户订单号
  40. * @param total_fee 订单总金额
  41. * @param refund_fee 退款金额
  42. * @return 退款结果(xml格式)
  43. */
  44. public string Refund(string transaction_id, string out_trade_no, string total_fee, string refund_fee)
  45. {
  46. // Log.Info("Refund", "Refund is processing...");
  47. WxPayData data = new WxPayData();
  48. if (!string.IsNullOrEmpty(transaction_id))//微信订单号存在的条件下,则已微信订单号为准
  49. {
  50. data.SetValue("transaction_id", transaction_id);
  51. }
  52. else//微信订单号不存在,才根据商户订单号去退款
  53. {
  54. data.SetValue("out_trade_no", out_trade_no);
  55. }
  56. data.SetValue("total_fee", int.Parse(total_fee));//订单总金额
  57. data.SetValue("refund_fee", int.Parse(refund_fee));//退款金额
  58. data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo());//随机生成商户退款单号
  59. data.SetValue("op_user_id", WxPayConfig.GetConfig().GetMchID());//操作员,默认为商户号
  60. WxPayData result = WxPayApi.Refund(data);//提交退款申请给API,接收返回数据
  61. // Log.Info("Refund", "Refund process complete, result : " + result.ToXml());
  62. return result.ToPrintStr();
  63. }
  64. }
  65. }