/// <summary> /// 构造支付请求数据 /// </summary> /// <param>订单名称</param> /// <param>订单金额</param> /// <param>订单编号</param> /// <returns>请求结果集</returns> private AlipayTradePrecreateContentBuilder BuildPrecreateContent(string orderName,string orderAmount,string outTradeNo) { //线上联调时,请输入真实的外部订单号。 if (string.IsNullOrEmpty(outTradeNo)) { outTradeNo = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "0000" + (new Random()).Next(1, 10000).ToString(); } AlipayTradePrecreateContentBuilder builder = new AlipayTradePrecreateContentBuilder(); //收款账号 builder.seller_id = Config.Uid; //订单编号 builder.out_trade_no = outTradeNo; //订单总金额 builder.total_amount = orderAmount; //参与优惠计算的金额 //builder.discountable_amount = ""; //不参与优惠计算的金额 //builder.undiscountable_amount = ""; //订单名称 builder.subject = orderName; //自定义超时时间 builder.timeout_express = "5m"; //订单描述 builder.body = ""; //门店编号,很重要的参数,可以用作之后的营销 builder.store_id = "test store id"; //操作员编号,很重要的参数,可以用作之后的营销 builder.operator_id = "test"; //传入商品信息详情 List<GoodsInfo> gList = new List<GoodsInfo>(); GoodsInfo goods = new GoodsInfo(); goods.goods_id = "goods id"; goods.goods_name = "goods name"; goods.price = "0.01"; goods.quantity = "1"; gList.Add(goods); builder.goods_detail = gList; //系统商接入可以填此参数用作返佣 //ExtendParams exParam = new ExtendParams(); //exParam.sysServiceProviderId = "20880000000000"; //builder.extendParams = exParam; return builder; }
3.渲染二维码
/// <summary> /// 渲染二维码 /// </summary> /// <param></param> /// <returns></returns> private Bitmap RenderQrCode(string str) { QRCodeGenerator.ECCLevel eccLevel = QRCodeGenerator.ECCLevel.L; using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) { using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(str, eccLevel)) { using (QRCode qrCode = new QRCode(qrCodeData)) { Bitmap bp= qrCode.GetGraphic(20, Color.Black, Color.White, new Bitmap(Path.Combine(_hostingEnvironment.WebRootPath, "images/alipay.png")), 15); return bp; } } } }
4.轮询支付结果
/// <summary> /// 轮询支付结果 /// </summary> /// <param>订单号</param> public void LoopQuery(object o) { AlipayF2FQueryResult queryResult = new AlipayF2FQueryResult(); int count = 100; int interval = 10000; string outTradeNo = o.ToString(); for (int i = 1; i <= count; i++) { Thread.Sleep(interval); queryResult = _serviceClient.tradeQuery(outTradeNo); if (queryResult?.Status == ResultEnum.SUCCESS) { DoSuccessProcess(queryResult); return; } } DoFailedProcess(queryResult); } /// <summary> /// 请添加支付成功后的处理 /// </summary> private void DoSuccessProcess(AlipayF2FQueryResult queryResult) { //支付成功,请更新相应单据 Console.WriteLine("扫码支付成功:商户订单号 " + queryResult.response.OutTradeNo); } /// <summary> /// 请添加支付失败后的处理 /// </summary> private void DoFailedProcess(AlipayF2FQueryResult queryResult) { //支付失败,请更新相应单据 Console.WriteLine("扫码支付失败:商户订单号 " + queryResult.response.OutTradeNo); }