VB.net开发微信、支付宝扫码支付源码_Vb.net支付宝


Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function StrAddStr Lib "PayApiFun.dll" (ByVal inputstr As String, ByRef outinf As IntPtr) As Integer
'微信支付函数,第一代账号要加密后才能调用
Private Declare Function WeiXinPayCodeEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal Lockmchid As String, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal AuthCode As String, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function WeiXinPayQRCodeEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal Lockmchid As String, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function WeiXinQueryEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal Lockmchid As String, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
Private Declare Function WeiXinCloseOrderEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal Lockmchid As String, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
'微信支付函数,第二代
Private Declare Function WeiXinPayCodeEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal mchid As String, ByVal registered As Integer, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal AuthCode As String, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function WeiXinPayQRCodeEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal mchid As String, ByVal registered As Integer, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function WeiXinQueryEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal mchid As String, ByVal registered As Integer, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
Private Declare Function WeiXinCloseOrderEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal mchid As String, ByVal registered As Integer, ByVal Idkey As String, ByVal IdkeyLock As Integer, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
'支付宝支付函数,第一代账号要加密后才能调用
Private Declare Function ZhiFuBaoPayCodeEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal AuthCode As String, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function ZhiFuBaoPayQRCodeEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function ZhiFuBaoQueryEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
Private Declare Function ZhiFuBaoPayCancelEx1 Lib "PayApiFun.dll" (ByVal Lockappid As String, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
'支付宝支付函数,第二代
Private Declare Function ZhiFuBaoPayCodeEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal registered As Integer, ByVal AuthCode As String, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function ZhiFuBaoPayQRCodeEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal registered As Integer, ByVal OrdNum As String, ByVal TotalFee As String, ByVal inbody As String, ByRef outinf As IntPtr) As Integer
Private Declare Function ZhiFuBaoQueryEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal registered As Integer, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer
Private Declare Function ZhiFuBaoPayCancelEx2 Lib "PayApiFun.dll" (ByVal appid As String, ByVal registered As Integer, ByVal OrdNum As String, ByRef outinf As IntPtr) As Integer

Private Declare Function PQRCodeEx Lib "PayApiFun.dll" (ByVal QRCodeStr As String) As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox7.Text = Format(Now(), "yyyyMMddHHMMss") + "00001"
PictureBox1.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Trim(TextBox2.Text) = "" Then
MsgBox("请输入要生成二维码的字符串信息!", vbCritical + vbOKOnly, "提示")
TextBox2.Select()
Exit Sub
Else
PQRCodeEx(Trim(TextBox2.Text))
PictureBox1.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp"
End If

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim real As Integer
Dim Lockappid As String
Dim Lockmchid As String
Dim Idkey As String
Dim IdkeyLock As Integer
Dim registered As Integer
Dim OrdNum As String
Dim outinf As IntPtr

Lockappid = Trim(WXGZH.Text) '微信公众号
Lockmchid = Trim(WXSFH.Text) '商户号
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 公众号、商户号已加密授权 =0未加密授权

Idkey = Trim(WXKey.Text) 'API密钥
If RadioButton2.Checked Then IdkeyLock = 1 Else IdkeyLock = 0 'IdkeyLock = 1 API密钥已加密 ,0未加密

outinf = Marshal.AllocHGlobal(255)

OrdNum = Trim(TextBox7.Text)
If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub

TextBox1.Text = ""

Button4.Enabled = False
Button4.Text = "正在查询中,请稍后..."
Application.DoEvents()

'real = WeiXinQueryEx1(Lockappid, Lockmchid, Idkey, IdkeyLock, OrdNum, outinf) '第一代函数
real = WeiXinQueryEx2(Lockappid, Lockmchid, registered, Idkey, IdkeyLock, OrdNum, outinf) '第二代函数

Select Case real
Case 0
TextBox1.Text = "微信支付单号‘" + OrdNum + "’支付成功!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case 1
TextBox1.Text = "微信支付单号‘" + OrdNum + "’正在支付...,请稍后查询此单支付状态,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "微信支付单号‘" + OrdNum + "’支付失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button4.Enabled = True
Button4.Text = "查询微信订单状态 Ex1"
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Lockappid As String
Dim Lockmchid As String
Dim Idkey As String
Dim IdkeyLock As Integer
Dim registered As Integer
Dim TotalFee As String
Dim inbody As String
Dim OrdNum As String
Dim outinf As IntPtr
Dim real As Integer

Lockappid = Trim(WXGZH.Text) '微信公众号
Lockmchid = Trim(WXSFH.Text) '商户号
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 公众号、商户号已加密授权 =0未加密授权

Idkey = Trim(WXKey.Text) 'API密钥
If RadioButton2.Checked Then IdkeyLock = 1 Else IdkeyLock = 0 'IdkeyLock = 1 API密钥已加密 ,0未加密

OrdNum = Trim(TextBox7.Text)
TotalFee = Trim(TextBox8.Text)
inbody = Trim(TextBox3.Text)
outinf = Marshal.AllocHGlobal(255)

TextBox1.Text = ""
TextBox2.Text = ""

If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub

Button2.Enabled = False
Button2.Text = "正在申请URL,请稍后..."
Application.DoEvents()

'real = WeiXinPayQRCodeEx1(Lockappid, Lockmchid, Idkey, IdkeyLock, OrdNum, TotalFee, inbody, outinf) '第一代函数
real = WeiXinPayQRCodeEx2(Lockappid, Lockmchid, registered, Idkey, IdkeyLock, OrdNum, TotalFee, inbody, outinf) '第二代函数

Select Case real
Case 0
TextBox2.Text = Marshal.PtrToStringAnsi(outinf).ToString()
TextBox1.Text = "微信支付单号‘" + OrdNum + "’支付二维码已经生成,有效时间为5分钟,可轮询调用‘查询微信订单状态’函数查询支付结果!"
PictureBox1.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp"
Case 1
TextBox1.Text = "微信支付单号‘" + OrdNum + "’正在申请支付二维码,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case -10
TextBox1.Text = "微信支付单号‘" + OrdNum + "’已返回URL,有效时间为5分钟,生成二维码时出现异常,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
TextBox2.Text = Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "微信支付单号‘" + OrdNum + "’申请支付URL失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button2.Enabled = True
Button2.Text = "生成微信支付二维码 Ex1"
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim Lockappid As String
Dim Lockmchid As String
Dim Idkey As String
Dim IdkeyLock As Integer
Dim registered As Integer
Dim TotalFee As String
Dim OrdNum As String
Dim AuthCode As String
Dim inbody As String
Dim outinf As IntPtr
Dim real As Integer

Lockappid = Trim(WXGZH.Text) '微信公众号
Lockmchid = Trim(WXSFH.Text) '商户号
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 公众号、商户号已加密授权 =0未加密授权

Idkey = Trim(WXKey.Text) 'API密钥
If RadioButton2.Checked Then IdkeyLock = 1 Else IdkeyLock = 0 'IdkeyLock = 1 API密钥已加密 ,0未加密

OrdNum = Trim(TextBox7.Text)
TotalFee = Trim(TextBox8.Text)
AuthCode = Trim(TextBox9.Text)
inbody = Trim(TextBox3.Text)
outinf = Marshal.AllocHGlobal(255)

TextBox1.Text = ""
TextBox2.Text = ""

If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub
If AuthCode = "" Then MsgBox("请扫描或输入18位微信付款码!", vbCritical + vbOKOnly, "提示") : TextBox9.Select() : Exit Sub

Button3.Enabled = False
Button3.Text = "正在支付中,请稍后..."
Application.DoEvents()

'real = WeiXinPayCodeEx1(Lockappid, Lockmchid, Idkey, IdkeyLock, AuthCode, OrdNum, TotalFee, inbody, outinf) '第一代函数
real = WeiXinPayCodeEx2(Lockappid, Lockmchid, registered, Idkey, IdkeyLock, AuthCode, OrdNum, TotalFee, inbody, outinf) '第二代函数

Select Case real
Case 0
TextBox1.Text = "微信支付单号‘" + OrdNum + "’支付成功!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case 1
TextBox1.Text = "微信支付单号‘" + OrdNum + "’正在支付...,请稍后查询此单支付状态,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "微信支付单号‘" + OrdNum + "’支付失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button3.Enabled = True
Button3.Text = "微信收付款码支付 Ex1"

End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim Lockappid As String
Dim registered As Integer
Dim OrdNum As String
Dim outinf As IntPtr
Dim real As Integer

Lockappid = Trim(ZFBappid.Text) '支付宝APPID
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 支付宝APPID已加密授权 =0未加密授权

outinf = Marshal.AllocHGlobal(255)
OrdNum = Trim(TextBox7.Text)
If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub

TextBox1.Text = ""
Button5.Enabled = False
Button5.Text = "正在查询中,请稍后..."
Application.DoEvents()

'real = ZhiFuBaoQueryEx1(Lockappid, OrdNum, outinf) '第一代函数
real = ZhiFuBaoQueryEx2(Lockappid, registered, OrdNum, outinf) '第二代函数

Select Case real
Case 0
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’支付成功!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case 1
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’正在支付...,请稍后查询此单支付状态,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’支付失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button5.Enabled = True
Button5.Text = "查询支付宝订单状态 Ex"

End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim Lockappid As String
Dim registered As Integer
Dim TotalFee As String
Dim OrdNum As String
Dim AuthCode As String
Dim inbody As String
Dim outinf As IntPtr
Dim real As Integer

Lockappid = Trim(ZFBappid.Text) '支付宝APPID
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 支付宝APPID已加密授权 =0未加密授权

OrdNum = Trim(TextBox7.Text)
TotalFee = Format(CLng(TextBox8.Text) / 100, "0.00") '单位为元,与微信单位不同,要注意
AuthCode = Trim(TextBox9.Text)
inbody = Trim(TextBox3.Text)
outinf = Marshal.AllocHGlobal(255)

TextBox1.Text = ""
TextBox2.Text = ""

If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub
If AuthCode = "" Then MsgBox("请扫描或输入有效的支付宝付款码!", vbCritical + vbOKOnly, "提示") : TextBox9.Select() : Exit Sub

Button6.Enabled = False
Button6.Text = "正在支付中,请稍后..."
Application.DoEvents()

'real = ZhiFuBaoPayCodeEx1(Lockappid, AuthCode, OrdNum, TotalFee, inbody, outinf) '第一代函数
real = ZhiFuBaoPayCodeEx2(Lockappid, registered, AuthCode, OrdNum, TotalFee, inbody, outinf) '第二代函数

Select Case real
Case 0
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’支付成功!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case 1
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’正在支付...,请稍后查询此单支付状态,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’支付失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button6.Enabled = True
Button6.Text = "支付宝付款码支付 Ex"

End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim Lockappid As String
Dim registered As Integer
Dim TotalFee As String
Dim OrdNum As String
Dim inbody As String
Dim outinf As IntPtr
Dim real As Integer

Lockappid = Trim(ZFBappid.Text) '支付宝APPID
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 支付宝APPID已加密授权 =0未加密授权

OrdNum = Trim(TextBox7.Text)
TotalFee = Format(CLng(TextBox8.Text) / 100, "0.00") '单位为元,与微信单位不同,要注意
inbody = Trim(TextBox3.Text)
outinf = Marshal.AllocHGlobal(255)

TextBox1.Text = ""
TextBox2.Text = ""

If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub

Button7.Enabled = False
Button7.Text = "正在申请URL,请稍后..."
Application.DoEvents()

'real = ZhiFuBaoPayQRCodeEx1(Lockappid, OrdNum, TotalFee, inbody, outinf) '第一代函数
real = ZhiFuBaoPayQRCodeEx2(Lockappid, registered, OrdNum, TotalFee, inbody, outinf) '第二代函数

Select Case real
Case 0
TextBox2.Text = Marshal.PtrToStringAnsi(outinf).ToString()
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’支付二维码已经生成,有效时间为5分钟,可轮询调用‘查询支付宝订单状态’函数查询支付结果!"
PictureBox1.ImageLocation = Environment.CurrentDirectory + "\\QRCode.bmp"
Case Else
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’申请支付URL失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button7.Enabled = True
Button7.Text = "生成支付宝支付二维码 Ex"

End Sub

Private Sub TextBox9_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox9.KeyPress

End Sub

Private Sub TextBox9_TextChanged(sender As Object, e As EventArgs) Handles TextBox9.TextChanged

End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Panel1.Visible = True
End Sub

Private Sub Button9_Click(sender As Object, e As EventArgs)
Panel1.Visible = False
End Sub

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
Dim real As Integer
Dim Lockappid As String
Dim Lockmchid As String
Dim Idkey As String
Dim IdkeyLock As Integer
Dim registered As Integer
Dim OrdNum As String
Dim outinf As IntPtr

Lockappid = Trim(WXGZH.Text) '微信公众号
Lockmchid = Trim(WXSFH.Text) '商户号
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 公众号、商户号已加密授权 =0未加密授权

Idkey = Trim(WXKey.Text) 'API密钥
If RadioButton2.Checked Then IdkeyLock = 1 Else IdkeyLock = 0 'IdkeyLock = 1 API密钥已加密 ,0未加密

outinf = Marshal.AllocHGlobal(255)

OrdNum = Trim(TextBox7.Text)
If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub

TextBox1.Text = ""
Button11.Enabled = False
Button11.Text = "正在关闭微信订单..."
Application.DoEvents()

'real = WeiXinCloseOrderEx1(Lockappid, Lockmchid, Idkey, IdkeyLock, OrdNum, outinf) '第一代函数
real = WeiXinCloseOrderEx2(Lockappid, Lockmchid, registered, Idkey, IdkeyLock, OrdNum, outinf) '第二代函数

Select Case real
Case 0
TextBox1.Text = "微信支付单号‘" + OrdNum + "’关闭成功!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "微信支付单号‘" + OrdNum + "’关闭失败,API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button11.Enabled = True
Button11.Text = "关闭未支付的微信订单 Ex1"
End Sub

Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
Dim Lockappid As String
Dim registered As Integer
Dim OrdNum As String
Dim outinf As IntPtr
Dim real As Integer

Lockappid = Trim(ZFBappid.Text) '支付宝APPID
If RadioButton3.Checked Then registered = 1 Else registered = 0 'registered = 1 支付宝APPID已加密授权 =0未加密授权

outinf = Marshal.AllocHGlobal(255)
OrdNum = Trim(TextBox7.Text)
If OrdNum = "" Then MsgBox("请输入商户号内的唯一订单编号!", vbCritical + vbOKOnly, "提示") : TextBox7.Select() : Exit Sub

TextBox1.Text = ""
Button10.Enabled = False
Button10.Text = "正在撒销支付宝订单..."
Application.DoEvents()

'real = ZhiFuBaoPayCancelEx1(Lockappid, OrdNum, outinf) '第一代函数
real = ZhiFuBaoPayCancelEx2(Lockappid, registered, OrdNum, outinf) '第二代函数

Select Case real
Case 0
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’还未支付,已撒销成功!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case 1
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’撒销成功!已触发退款动作。API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
Case Else
TextBox1.Text = "支付宝支付单号‘" + OrdNum + "’撒销失败!API接口返回的信息:" + Marshal.PtrToStringAnsi(outinf).ToString()
End Select

Button10.Enabled = True
Button10.Text = "撤销支付宝支付订单 Ex1"
End Sub

Private Sub Button9_Click_1(sender As Object, e As EventArgs) Handles Button9.Click
Panel1.Visible = False
End Sub
End Class