⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pmalipay.vb

📁 C#语言制作asp.net网上商店的
💻 VB
字号:
Imports System
Imports System.Data
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Web
Imports System.Web.Security
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Security.Cryptography
Imports NetShopForge.Library.Order
Imports NetShopForge.Common

Namespace NetShopForge.Library.Payment
    ''' <summary>
    ''' 支付宝网关接口
    ''' </summary>
    ''' <remarks></remarks>
    Public Class pmAlipay
        Implements IPayment

        Public Function Recive(ByVal page As System.Web.UI.Page) As NetShopForge.Library.Order.OrderTransactionInfo Implements IPayment.Recive
            Dim objPC As New PaymentController

            Dim pay As PaymentMethodInfo = objPC.GetPaymentMethodInfo(PaymentGateway.gwAliPay)
            Dim alipayNotifyURL As String = "http://www.alipay.com/cooperate/gateway.do?"
            Dim partner As String = pay.MerchantID   'partner合作伙伴id(必须填写)
            alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "&notify_id=" + page.Request.Form("notify_id")

            '获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
            Dim responseTxt As String = Get_Http(alipayNotifyURL, 120000)

            Dim key As String = pay.MerchantKey  'partner 的对应交易安全校验码(必须填写)
            Dim total_fee As String = page.Request.Form("total_fee") '商品单价
            Dim out_trade_no As String = page.Request.Form("out_trade_no") '外部交易号
            Dim notify_time As String = page.Request.Form("notify_time") '通知时间
            Dim trade_status As String = page.Request.Form("trade_status") '交易状态

            Dim i As Integer
            Dim coll As NameValueCollection
            'Load Form variables into NameValueCollection variable.
            coll = page.Request.Form
            ' Get names of all forms into a string array.
            Dim requestarr() As String = coll.AllKeys
            '进行排序;
            Dim Sortedstr() As String = BubbleSort(requestarr)
            '构造待md5摘要字符串 ;
            Dim prestr As String = ""
            For i = 0 To Sortedstr.Length - 1 Step i + 1
                If page.Request.Form(Sortedstr(i)) <> "" And Sortedstr(i) <> "sign" And Sortedstr(i) <> "sign_type" Then
                    If i = Sortedstr.Length - 1 Then
                        prestr = prestr + Sortedstr(i) + "=" + page.Request.Form(Sortedstr(i))
                    Else
                        prestr = prestr + Sortedstr(i) + "=" + page.Request.Form(Sortedstr(i)) + "&"
                    End If
                End If
            Next
            prestr = prestr + key

            Dim mysign As String = GetMD5(prestr)
            Dim sign As String = page.Request.Form("sign")

            If mysign = sign And responseTxt = "true" Then


                Dim objOT As New OrderTransactionInfo

                Return objOT
            End If
            Return Nothing

        End Function

        Public Function Send(ByVal orderID As Integer) As String Implements IPayment.Send
            '  Dim objOC As New OrderController
            Dim objPC As New PaymentController
            Dim order As OrderInfo = OrderController.GetOrder(orderID)
            Dim pay As PaymentMethodInfo = objPC.GetPaymentMethodInfo(PaymentGateway.gwAliPay)
            Dim URL As String = CreatUrl("rippleyong@126.com", order, pay)
            Return objPC.WriteSendString(URL)
        End Function


#Region "--------Private Method----"
        Private Function GetMD5(ByVal s As String) As String
            Dim md5 As New MD5CryptoServiceProvider()
            Dim t() As Byte = md5.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(s))
            Dim sb As New StringBuilder(32)
            Dim i As Integer
            For i = 0 To t.Length - 1 Step i + 1
                sb.Append(t(i).ToString("x").PadLeft(2, "0"c))
            Next
            Return sb.ToString()
        End Function

        Public Shared Function BubbleSort(ByVal R() As String) As String()
            '/ <summary>
            '/ 冒泡排序法
            '/ </summary>
            Dim i, j As Integer '交换标志 
            Dim temp As String

            Dim exchange As Boolean

            For i = 0 To R.Length - 1 '最多做R.Length-1趟排序 
                exchange = False '本趟排序开始前,交换标志应为假
                For j = R.Length - 2 To i Step -1
                    If System.String.CompareOrdinal(R((j + 1)), R(j)) < 0 Then '交换条件
                        temp = R((j + 1))
                        R((j + 1)) = R(j)
                        R(j) = temp

                        exchange = True '发生了交换,故将交换标志置为真 
                    End If
                Next j

                If Not exchange Then '本趟排序未发生交换,提前终止算法 
                    Exit For
                End If
            Next i
            Return R
        End Function 'BubbleSort
        Private Function CreatUrl(ByVal sellerEmail As String, ByVal order As OrderInfo, ByVal payInfo As PaymentMethodInfo) As String
            Dim i As Integer

            '业务参数赋值;
            Dim gateway As String = "http://www.alipay.com/cooperate/gateway.do?"  '支付接口
            Dim service As String = "trade_create_by_buyer"  '外部接口名称,如:user_query
            Dim partner As String = payInfo.MerchantID  '合作伙伴在支付宝的用户ID		
            Dim sign_type As String = "MD5"
            Dim subject As String = String.Format("订单号:{0}", order.OrderNumber) 'subject		商品名称
            Dim body As String = "NETSHOPFORGE订单"  'body			商品描述
            Dim price As String = order.ProductSubtotalFee  'price			商品单价 0.01~50000.00
            Dim discount As String = order.DiscountFee ' "0" '折扣
            Dim show_url As String = Utility.GetSiteRoot & "/PaymetRecive.aspx?OrderID=1" ''"http://www.alipay.com" '商品展示网址
            Dim quantity As String = "1" '购买数量
            Dim payment_type As String = "1" '
            Dim logistics_type As String = "EXPRESS" '
            Dim logistics_fee As String = order.ShippingFee ' "0" '
            Dim logistics_payment As String = "SELLER_PAY" '
            'Dim logistics_type_1 As String = "EMS"
            'Dim logistics_fee_1 As String = "4"
            'Dim logistics_payment_1 As String = "SELLER_PAY"
            Dim seller_email As String = sellerEmail 'payInfo.MerchantID  '""  '*****卖家账号
            Dim key As String = payInfo.MerchantKey ' ""  '******partner账户的支付宝安全校验码
            Dim notify_url As String = Utility.GetSiteRoot & "/PaymetRecive.aspx?PayType=" & System.Enum.GetName(GetType(PaymentGateway), payInfo.PaymentGateway)

            '构造数组;
            Dim Oristr() As String = {"service=" + service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + order.OrderNumber, "price=" + price, "discount=" + discount, "show_url=" + show_url, "quantity=" + quantity, "payment_type=" + payment_type, "logistics_type=" + logistics_type, "logistics_fee=" + logistics_fee, "logistics_payment=" + logistics_payment, "seller_email=" + seller_email, "notify_url=" + notify_url}


            '进行排序;
            Dim Sortedstr As String() = BubbleSort(Oristr)


            '构造待md5摘要字符串 ;
            Dim prestr As String = ""
            For i = 0 To Sortedstr.Length - 1
                If i = Sortedstr.Length - 1 Then
                    prestr = prestr + Sortedstr(i)
                Else
                    prestr = prestr + Sortedstr(i) + "&"
                End If
            Next i
            prestr = prestr + key

            '生成Md5摘要;
            Dim sign As String = GetMD5(prestr)

            '构造支付Url;
            Dim parameter As String = ""
            parameter = parameter + gateway
            For i = 0 To Sortedstr.Length - 1
                parameter = parameter + Sortedstr(i) + "&"
            Next i

            parameter = parameter + "sign=" + sign + "&sign_type=" + sign_type

            '返回支付Url;
            Return parameter
        End Function 'CreatUrl 
        Public Function Get_Http(ByVal a_strUrl As String, ByVal timeout As Integer) As String
            Dim strResult As String
            Try

                Dim myReq As HttpWebRequest = CType(HttpWebRequest.Create(a_strUrl), HttpWebRequest)
                myReq.Timeout = timeout
                Dim HttpWResp As HttpWebResponse = CType(myReq.GetResponse(), HttpWebResponse)
                Dim myStream As Stream = HttpWResp.GetResponseStream()
                Dim sr As StreamReader = New StreamReader(myStream, Encoding.Default)
                Dim strBuilder As StringBuilder = New StringBuilder()
                While -1 <> sr.Peek()
                    strBuilder.Append(sr.ReadLine())
                End While

                strResult = strBuilder.ToString()
            Catch exp As Exception

                strResult = "错误:" + exp.Message
            End Try

            Return strResult
        End Function

#End Region





    End Class

End Namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -