最后一步 创建界面 测试
(未经允许!不得转载!转载也行!请加链接!)
using AlipayRelevant; using Aop.Api.Request; using Aop.Api.Response; using Com.Alipay; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; //http://www.cnblogs.com/CigaretteBeauty/ namespace FaceToFacePayment { public partial class Form1 : Form { public Form1() { //http://www.cnblogs.com/CigaretteBeauty/ InitializeComponent(); } NewClass newClass = new NewClass(); private void button1_Click(object sender, EventArgs e) { //http://www.cnblogs.com/CigaretteBeauty/ AlipayTradePrecreateResponse payResponse; string result = ScanCode(out payResponse); this.textBox1.Text = result; pictureBox1.Image = newClass.GenByZXingNet(payResponse.QrCode); } /// <summary> /// 扫码支付 /// </summary> /// <param name="payResponse">返回二维码字符串</param> /// <param name="result">返回结果</param> private string ScanCode(out AlipayTradePrecreateResponse payResponse) { //线上联调时,请输入真实的外部订单号。 string out_trade_no = DateTime.Now.ToString("yyyyMMddHHmmss") + "0000" + (new Random()).Next(1, 10000).ToString(); //商户唯一订单号 string subject = WIDsubject.Text.Trim(); //商品名称 string total_amount = WIDamount.Text.Trim(); //金额 //请根据实际请求需要biz_content参数 StringBuilder sb = new StringBuilder(); sb.Append("{\"out_trade_no\":\"" + out_trade_no + "\","); sb.Append("\"total_amount\":\"" + total_amount + "\",\"discountable_amount\":\"0.00\","); sb.Append("\"subject\":\"" + subject + "\",\"body\":\"test\","); sb.Append("\"goods_detail\":[{\"goods_id\":\"items01\",\"goods_name\":\"XX酒店消费\",\"goods_category\":\"000001\",\"price\":\"88.00\",\"quantity\":\"1\"}],"); sb.Append("\"operator_id\":\"op001\",\"store_id\":\"pudong001\",\"terminal_id\":\"t_001\","); string expire_time = System.DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss"); sb.Append("\"time_expire\":\"" + expire_time + "\"}"); payResponse = newClass.Prepay(sb.ToString()); //以下返回结果的处理供参考。 //payResponse.QrCode即二维码对于的链接 //将链接用二维码工具生成二维码打印出来,顾客可以用支付宝钱包扫码支付。 string result = payResponse.Body; if (payResponse != null) { switch (payResponse.Code) { case ResultCode.SUCCESS: System.Console.Write(" 预下单成功"); StringBuilder sb1 = new StringBuilder(); sb1.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); break; case ResultCode.FAIL: StringBuilder sb2 = new StringBuilder(); sb2.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); newClass.Cancel(sb2.ToString()); break; } } return result; } private void button2_Click(object sender, EventArgs e) { this.textBox3.Text = BarCode(); } private string BarCode() { //线上联调时,请输入真实的外部订单号。 string out_trade_no = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "0000" + (new Random()).Next(1, 10000).ToString(); //商户唯一订单号 //线上联调时,请输入真实的条码。 string auth_code = TXTBarCode.Text.Trim(); if (String.IsNullOrEmpty(auth_code)) { MessageBox.Show("扫码失败!"); return "扫码失败"; } string total_amount = TXTMoney.Text.Trim(); StringBuilder sb = new StringBuilder(); sb.Append("{\"out_trade_no\":\"" + out_trade_no + "\","); sb.Append("\"scene\":\"bar_code\","); sb.Append("\"auth_code\":\"" + auth_code + "\","); sb.Append("\"total_amount\":\"" + total_amount + "\",\"discountable_amount\":\"0.00\","); sb.Append("\"subject\":\"泰能软件-条码支付\",\"body\":\"abc\","); sb.Append("\"goods_detail\":[{\"goods_id\":\"items01\",\"goods_name\":\"XX酒店消费\",\"goods_category\":\"000001\",\"price\":\"" + total_amount + "\",\"quantity\":\"1\"}],"); sb.Append("\"operator_id\":\"op001\",\"store_id\":\"pudong001\",\"terminal_id\":\"t_001\","); string expire_time = System.DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss"); sb.Append("\"time_expire\":\"" + expire_time + "\"}"); AlipayTradePayResponse payResponse = newClass.Pay(sb.ToString()); string result = payResponse.Body; if (payResponse != null) { switch (payResponse.Code) { case ResultCode.SUCCESS: System.Console.Write("支付成功"); result = payResponse.Body; break; case ResultCode.INRROCESS: StringBuilder sb1 = new StringBuilder(); sb1.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); //根据业务需要,选择是否新起线程进行轮询 //ParameterizedThreadStart ParStart = new ParameterizedThreadStart(LoopQuery); //Thread myThread = new Thread(ParStart); //object o = payResponse; //myThread.Start(o); //返回支付处理中,需要进行轮询 AlipayTradeQueryResponse queryResponse =newClass.LoopQuery(sb1.ToString()); //用订单号trade_no进行轮询也是可以的。 if (queryResponse != null) { result = queryResponse.Body; } break; case ResultCode.FAIL: StringBuilder sb2 = new StringBuilder(); sb2.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); newClass.Cancel(sb2.ToString()); break; } } return result; } private void button3_Click(object sender, EventArgs e) { StringBuilder sb1 = new StringBuilder(); sb1.Append("{\"out_trade_no\":\"" + this.textBox2.Text + "\"}"); AlipayTradeQueryResponse queryResponse = newClass.LoopQuery(sb1.ToString()); //用订单号trade_no进行轮询也是可以的。 if (queryResponse != null) { this.textBox4.Text = "\r\n" + queryResponse.Body; } } private void button4_Click(object sender, EventArgs e) { AlipayTradeQueryRequest payRequst = new AlipayTradeQueryRequest(); AlipayTradeQueryResponse payResponse = null; for (int i = 1; i <= 6; i++) { //Thread.Sleep(5000); payResponse = newClass.client.Execute(payRequst); if (string.Compare(payResponse.Code, ResultCode.SUCCESS, false) == 0) { if (payResponse.TradeStatus == "TRADE_FINISHED" || payResponse.TradeStatus == "TRADE_SUCCESS" || payResponse.TradeStatus == "TRADE_CLOSED") break; } } StringBuilder sb1 = new StringBuilder(); sb1.Append("{\"out_trade_no\":\"" + this.textBox2.Text + "\"}"); this.textBox4.Text = newClass.Cancel(sb1.ToString()).Body; } } }
时间: 2024-11-08 14:17:10