当前位置:   article > 正文

C#微信、支付宝扫码支付源码_c#支付宝微信支付demo

c#支付宝微信支付demo

  扫码消费机介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.13.73c351246rDatj&id=17021194999https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.13.73c351246rDatj&id=17021194999

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices; //调用动态库一定要加入这个引用
  10. using System.Text;//一定要加入这个
  11. namespace PayTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. [DllImport("PayApiFun.dll", EntryPoint = "StrAddStr", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  16. static extern int StrAddStr(string inputstr,ref IntPtr outputstr);
  17. [DllImport("PayApiFun.dll", EntryPoint = "PQRCodeEx", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  18. static extern int PQRCodeEx(string inputstr);
  19. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinPayCodeEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  20. static extern int WeiXinPayCodeEx1(string Lockappid, string Lockmchid, string Idkey, int IdkeyLock, string AuthCode, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  21. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinPayQRCodeEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  22. static extern int WeiXinPayQRCodeEx1(string Lockappid, string Lockmchid, string Idkey, int IdkeyLock, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  23. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinQueryEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  24. static extern int WeiXinQueryEx1(string Lockappid, string Lockmchid, string Idkey, int IdkeyLock, string OrdNum, ref IntPtr outputstr);
  25. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinCloseOrderEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  26. static extern int WeiXinCloseOrderEx1(string Lockappid, string Lockmchid, string Idkey, int IdkeyLock, string OrdNum, ref IntPtr outputstr);
  27. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoPayCodeEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  28. static extern int ZhiFuBaoPayCodeEx1(string Lockappid, string AuthCode, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  29. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoPayQRCodeEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  30. static extern int ZhiFuBaoPayQRCodeEx1(string Lockappid, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  31. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoQueryEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  32. static extern int ZhiFuBaoQueryEx1(string Lockappid, string OrdNum, ref IntPtr outputstr);
  33. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoPayCancelEx1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  34. static extern int ZhiFuBaoPayCancelEx1(string Lockappid, string OrdNum, ref IntPtr outputstr);
  35. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinPayCodeEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  36. static extern int WeiXinPayCodeEx2(string appid, string mchid, int registered, string Idkey, int IdkeyLock, string AuthCode, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  37. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinPayQRCodeEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  38. static extern int WeiXinPayQRCodeEx2(string appid, string mchid, int registered, string Idkey, int IdkeyLock, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  39. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinQueryEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  40. static extern int WeiXinQueryEx2(string appid, string mchid, int registered, string Idkey, int IdkeyLock, string OrdNum, ref IntPtr outputstr);
  41. [DllImport("PayApiFun.dll", EntryPoint = "WeiXinCloseOrderEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  42. static extern int WeiXinCloseOrderEx2(string appid, string mchid, int registered, string Idkey, int IdkeyLock, string OrdNum, ref IntPtr outputstr);
  43. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoPayCodeEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  44. static extern int ZhiFuBaoPayCodeEx2(string appid, int registered, string AuthCode, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  45. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoPayQRCodeEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  46. static extern int ZhiFuBaoPayQRCodeEx2(string appid, int registered, string OrdNum, string TotalFee, string inbody, ref IntPtr outputstr);
  47. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoQueryEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  48. static extern int ZhiFuBaoQueryEx2(string appid, int registered, string OrdNum, ref IntPtr outputstr);
  49. [DllImport("PayApiFun.dll", EntryPoint = "ZhiFuBaoPayCancelEx2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  50. static extern int ZhiFuBaoPayCancelEx2(string appid, int registered, string OrdNum, ref IntPtr outputstr);
  51. public Form1()
  52. {
  53. InitializeComponent();
  54. }
  55. private void button1_Click(object sender, EventArgs e)
  56. {
  57. }
  58. private void button2_Click(object sender, EventArgs e)
  59. {
  60. string lockappid = WXAPPID.Text.Trim(); //微信公众号
  61. string lockmachid = WXMCHID.Text.Trim(); //商户号
  62. int registered = 0; //registered = 0 原始的公众号、商户号 =1 已加密授权的公众号、商户号
  63. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  64. string idkey = WXKEY.Text.Trim(); //API密钥
  65. int lockkey = 0; //lockkey = 0 原始API密钥 =1 加密后的API密钥
  66. if (radioButton1.Checked == true) { lockkey = 0; } else { lockkey = 1; }
  67. string OrdNum = textBox2.Text.Trim();
  68. string TotalFee = textBox3.Text.Trim();
  69. string inbody = textBox6.Text.Trim();
  70. IntPtr outputs = Marshal.AllocHGlobal(255);
  71. textBox1.Text = "";
  72. textBox5.Text = "";
  73. button2.Text = "正在申请支付URL...";
  74. Application.DoEvents();
  75. //int real = WeiXinPayQRCodeEx1(lockappid, lockmachid, idkey, lockkey, OrdNum,TotalFee,inbody, ref outputs); //第一代函数
  76. int real = WeiXinPayQRCodeEx2(lockappid, lockmachid,registered, idkey, lockkey, OrdNum,TotalFee,inbody, ref outputs); //第二代函数
  77. switch (real)
  78. {
  79. case 0:
  80. textBox1.Text = "微信支付单号:" + OrdNum + ",支付二维码已经生成,有效时间5分钟,可轮询调用‘查询微信订单状态’函数查询支付结果!";
  81. textBox5.Text = Marshal.PtrToStringAnsi(outputs).ToString();
  82. pictureBox2.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp";
  83. break;
  84. case 1:
  85. textBox1.Text = "微信支付单号:" + OrdNum + ",正在申请支付二维码,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  86. break;
  87. case -10:
  88. textBox1.Text = "微信支付单号:" + OrdNum + ",支付二维码URL:" + Marshal.PtrToStringAnsi(outputs).ToString();
  89. break;
  90. default :
  91. textBox1.Text = "微信支付单号:" + OrdNum + ",申请支付URL失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  92. break;
  93. }
  94. button2.Text = "生成微信支付二维码 Ex1";
  95. }
  96. private void Form1_Load(object sender, EventArgs e)
  97. {
  98. pictureBox2.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp";
  99. textBox2.Text = DateTime.Now.ToString("yyyyMMddHHMMss")+"0001";
  100. }
  101. private void button4_Click(object sender, EventArgs e)
  102. {
  103. string lockappid = WXAPPID.Text.Trim(); //微信公众号
  104. string lockmachid = WXMCHID.Text.Trim(); //商户号
  105. int registered = 0; //registered = 0 原始的公众号、商户号 =1 已加密授权的公众号、商户号
  106. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  107. string idkey = WXKEY.Text.Trim(); //API密钥
  108. int lockkey = 0; //lockkey = 0 原始API密钥 =1 加密后的API密钥
  109. if (radioButton1.Checked == true) { lockkey = 0; } else { lockkey = 1; }
  110. string OrdNum = textBox2.Text.Trim ();
  111. IntPtr outputs = Marshal.AllocHGlobal(255);
  112. textBox1.Text = "";
  113. button4.Text = "正在查询支付结果...";
  114. Application.DoEvents();
  115. //int real = WeiXinQueryEx1(lockappid, lockmachid, idkey, lockkey, OrdNum, ref outputs); //第一代函数
  116. int real = WeiXinQueryEx2(lockappid, lockmachid,registered, idkey, lockkey, OrdNum, ref outputs); //第二代函数
  117. switch (real)
  118. {
  119. case 0:
  120. textBox1.Text = "微信支付单号:" + OrdNum + ",支付成功,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  121. break;
  122. case 1:
  123. textBox1.Text = "微信支付单号:" + OrdNum + ",正在支付中,请稍后查询支付状态,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  124. break;
  125. default:
  126. textBox1.Text = "微信支付单号:" + OrdNum + ",支付失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  127. break;
  128. }
  129. button4.Text = "查询微信订单状态 Ex1";
  130. }
  131. private void button9_Click(object sender, EventArgs e)
  132. {
  133. panel1.Visible = false;
  134. }
  135. private void button8_Click(object sender, EventArgs e)
  136. {
  137. panel1.Visible =true ;
  138. }
  139. private void button3_Click(object sender, EventArgs e)
  140. {
  141. string lockappid = WXAPPID.Text.Trim(); //微信公众号
  142. string lockmachid = WXMCHID.Text.Trim(); //商户号
  143. int registered = 0; //registered = 0 原始的公众号、商户号 =1 已加密授权的公众号、商户号
  144. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  145. string idkey = WXKEY.Text.Trim(); //API密钥
  146. int lockkey = 0; //lockkey = 0 原始API密钥 =1 加密后的API密钥
  147. if (radioButton1.Checked == true) { lockkey = 0; } else { lockkey = 1; }
  148. string AuthCode = textBox4.Text.Trim();
  149. string OrdNum = textBox2.Text.Trim();
  150. string TotalFee = textBox3.Text.Trim();
  151. string inbody = textBox6.Text.Trim();
  152. IntPtr outputs = Marshal.AllocHGlobal(255);
  153. if(AuthCode=="")
  154. {
  155. MessageBox.Show("请输入正确的微信付款码!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
  156. return;
  157. }
  158. textBox1.Text = "";
  159. textBox5.Text = "";
  160. button3.Text = "正在申请微信支付...";
  161. Application.DoEvents();
  162. //int real = WeiXinPayCodeEx1(lockappid, lockmachid, idkey, lockkey, AuthCode, OrdNum, TotalFee, inbody, ref outputs); //第一代函数
  163. int real = WeiXinPayCodeEx2(lockappid, lockmachid,registered, idkey, lockkey, AuthCode, OrdNum, TotalFee, inbody, ref outputs); //第二代函数
  164. switch (real)
  165. {
  166. case 0:
  167. textBox1.Text = "微信支付单号:" + OrdNum + ",支付成功,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  168. break;
  169. case 1:
  170. textBox1.Text = "微信支付单号:" + OrdNum + ",正在支付中,请稍后查询支付状态,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  171. break;
  172. default:
  173. textBox1.Text = "微信支付单号:" + OrdNum + ",支付失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  174. break;
  175. }
  176. button3.Text = "微信收付款码支付 Ex1";
  177. }
  178. private void button7_Click(object sender, EventArgs e)
  179. {
  180. string lockappid = ZFBAPPID.Text.Trim(); //支付宝APPID
  181. int registered = 0; //registered = 0 原始的支付宝APPID =1 已加密授权的支付宝APPID
  182. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  183. string OrdNum = textBox2.Text.Trim();
  184. IntPtr outputs = Marshal.AllocHGlobal(255);
  185. textBox1.Text = "";
  186. button7.Text = "正在查询支付结果...";
  187. Application.DoEvents();
  188. //int real = ZhiFuBaoQueryEx1(lockappid, OrdNum, ref outputs); //第一代函数
  189. int real = ZhiFuBaoQueryEx2(lockappid,registered, OrdNum, ref outputs); //第二代函数
  190. switch (real)
  191. {
  192. case 0:
  193. textBox1.Text = "支付宝支付单号:" + OrdNum + ",支付成功,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  194. break;
  195. case 1:
  196. textBox1.Text = "支付宝支付单号:" + OrdNum + ",正在支付中,请稍后查询支付状态,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  197. break;
  198. default:
  199. textBox1.Text = "支付宝支付单号:" + OrdNum + ",支付失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  200. break;
  201. }
  202. button7.Text = "查询支付宝订单状态 Ex";
  203. }
  204. private void button6_Click(object sender, EventArgs e)
  205. {
  206. string lockappid = ZFBAPPID.Text.Trim(); //支付宝APPID
  207. int registered = 0; //registered = 0 原始的支付宝APPID =1 已加密授权的支付宝APPID
  208. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  209. string AuthCode = textBox4.Text.Trim();
  210. string OrdNum = textBox2.Text.Trim();
  211. string TotalFee = (Convert.ToDouble(textBox3.Text) / 100).ToString();
  212. string inbody = textBox6.Text.Trim();
  213. IntPtr outputs = Marshal.AllocHGlobal(255);
  214. if (AuthCode == "")
  215. {
  216. MessageBox.Show("请输入正确的支付宝付款码!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
  217. return;
  218. }
  219. textBox1.Text = "";
  220. textBox5.Text = "";
  221. button6.Text = "正在申请支付...";
  222. Application.DoEvents();
  223. //int real = ZhiFuBaoPayCodeEx1(lockappid, AuthCode, OrdNum, TotalFee, inbody, ref outputs); //第一代函数
  224. int real = ZhiFuBaoPayCodeEx2(lockappid,registered, AuthCode, OrdNum, TotalFee, inbody, ref outputs); //第二代函数
  225. switch (real)
  226. {
  227. case 0:
  228. textBox1.Text = "支付宝支付单号:" + OrdNum + ",支付成功,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  229. break;
  230. case 1:
  231. textBox1.Text = "支付宝支付单号:" + OrdNum + ",正在支付中,请稍后查询支付状态,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  232. break;
  233. default:
  234. textBox1.Text = "支付宝支付单号:" + OrdNum + ",支付失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  235. break;
  236. }
  237. button6.Text = "支付宝付款码支付 Ex1";
  238. }
  239. private void button5_Click(object sender, EventArgs e)
  240. {
  241. string lockappid = ZFBAPPID.Text.Trim(); //支付宝APPID
  242. int registered = 0; //registered = 0 原始的支付宝APPID =1 已加密授权的支付宝APPID
  243. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  244. string OrdNum = textBox2.Text.Trim();
  245. string TotalFee =(Convert.ToDouble (textBox3.Text)/100).ToString ();
  246. string inbody = textBox6.Text.Trim();
  247. IntPtr outputs = Marshal.AllocHGlobal(255);
  248. textBox1.Text = "";
  249. textBox5.Text = "";
  250. button5.Text = "正在申请支付URL...";
  251. Application.DoEvents();
  252. //int real = ZhiFuBaoPayQRCodeEx1(lockappid, OrdNum, TotalFee, inbody, ref outputs); //第一代函数
  253. int real = ZhiFuBaoPayQRCodeEx2(lockappid,registered, OrdNum, TotalFee, inbody, ref outputs); //第二代函数
  254. switch (real)
  255. {
  256. case 0:
  257. textBox1.Text = "支付宝支付单号:" + OrdNum + ",支付二维码已经生成,有效时间5分钟,可轮询调用‘查询支付宝订单状态’函数查询支付结果!";
  258. textBox5.Text = Marshal.PtrToStringAnsi(outputs).ToString();
  259. pictureBox2.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp";
  260. break;
  261. default:
  262. textBox1.Text = "支付宝支付单号:" + OrdNum + ",申请支付URL失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  263. break;
  264. }
  265. button5.Text = "生成支付宝支付二维码 Ex";
  266. }
  267. private void button11_Click(object sender, EventArgs e)
  268. {
  269. string lockappid = WXAPPID.Text.Trim(); //微信公众号
  270. string lockmachid = WXMCHID.Text.Trim(); //商户号
  271. int registered = 0; //registered = 0 原始的公众号、商户号 =1 已加密授权的公众号、商户号
  272. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  273. string idkey = WXKEY.Text.Trim(); //API密钥
  274. int lockkey = 0; //lockkey = 0 原始API密钥 =1 加密后的API密钥
  275. if (radioButton1.Checked == true) { lockkey = 0; } else { lockkey = 1; }
  276. string OrdNum = textBox2.Text.Trim();
  277. IntPtr outputs = Marshal.AllocHGlobal(255);
  278. textBox1.Text = "";
  279. button11.Text = "正在关闭微信订单...";
  280. Application.DoEvents();
  281. //int real = WeiXinCloseOrderEx1(lockappid, lockmachid, idkey, lockkey, OrdNum, ref outputs); //第一代函数
  282. int real = WeiXinQueryEx2(lockappid, lockmachid,registered, idkey, lockkey, OrdNum, ref outputs); //第二代函数
  283. switch (real)
  284. {
  285. case 0:
  286. textBox1.Text = "微信支付单号:" + OrdNum + ",关闭成功,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  287. break;
  288. default:
  289. textBox1.Text = "微信支付单号:" + OrdNum + ",关闭失败,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  290. break;
  291. }
  292. button11.Text = "关闭未支付的微信订单 Ex1";
  293. }
  294. private void button10_Click(object sender, EventArgs e)
  295. {
  296. string lockappid = ZFBAPPID.Text.Trim(); //支付宝APPID
  297. int registered = 0; //registered = 0 原始的支付宝APPID =1 已加密授权的支付宝APPID
  298. if (radioButton4.Checked == true) { registered = 0; } else { registered = 1; }
  299. string OrdNum = textBox2.Text.Trim();
  300. IntPtr outputs = Marshal.AllocHGlobal(255);
  301. textBox1.Text = "";
  302. button10.Text = "正在撒销支付宝订单...";
  303. Application.DoEvents();
  304. //int real = ZhiFuBaoPayCancelEx1(lockappid, OrdNum, ref outputs); //第一代函数
  305. int real = ZhiFuBaoPayCancelEx2(lockappid,registered, OrdNum, ref outputs); //第二代函数
  306. switch (real)
  307. {
  308. case 0:
  309. textBox1.Text = "支付宝支付单号:" + OrdNum + ",还未支付,已撒销成功!API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  310. break;
  311. case 1:
  312. textBox1.Text = "支付宝支付单号:" + OrdNum + ",撒销成功!已触发退款动作,API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  313. break;
  314. default:
  315. textBox1.Text = "支付宝支付单号:" + OrdNum + ",撒销失败!API接口返回的信息是:" + Marshal.PtrToStringAnsi(outputs).ToString();
  316. break;
  317. }
  318. button10.Text = "撒销支付宝订单 Ex1";
  319. }
  320. private void button9_Click_1(object sender, EventArgs e)
  321. {
  322. panel1.Visible = false;
  323. }
  324. }
  325. }

C#微信、支付宝扫码支付源码.rar-C#文档类资源-CSDN下载c#开发的微信、支付宝支付源代码,无需域名,只需输入微信公众号、微信商户号、微信API密钥;支付宝A更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/zhangjin7422/14947197?spm=1001.2014.3001.5503

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号