📜 ⬆️ ⬇️

Integration with Robokassa (ASP.NET)

Task: To organize the reception of electronic money.
There was a certain project that meant taking money from users for certain services. The robocash desk was chosen as a weaning tool - a resource that is serious, convenient, and developing (recently screwed up with SMS payments, is present, but is not active yet, payment by credit cards).

So, we are registered .

After registration, we indicate some data for interaction with the robocassan:

The idea of ​​the site: Every user registers, replenishes his balance on the site, and, in fact, spends this money on services.

User has registered.
')
To replenish the balance: for this you need to send a peculiarly generated request to the URL of the robocash desk. To send a request I use the following class:
Public Class RemotePost
Private Inputs As System.Collections.Specialized.NameValueCollection = New System.Collections.Specialized.NameValueCollection

Public Url As String = ""
Public Method As String = "post"
Public FormName As String = "form1"
Public Sub Add ( ByVal name As String , ByVal value As String )
Inputs.Add (name, value)
End sub
Public Sub Post ()
System.Web.HttpContext.Current.Response.Clear ()
System.Web.HttpContext.Current.Response.Write ( "<html> <head>" )
System.Web.HttpContext.Current.Response.Write ( String .Format ( "</ head> <body onload =" "document. {0} .submit ()" ">" , FormName))
System.Web.HttpContext.Current.Response.Write ( String .Format ( "<form name =" "{0}" "method =" "{1}" "action =" "{2}" ">" , FormName , Method, Url))
Dim i As Integer = 0
Do While i <Inputs.Keys.Count
System.Web.HttpContext.Current.Response.Write ( String .Format ( "<input name =" "{0}" "type =" "hidden" "value =" "{1}" ">" , Inputs.Keys (i) Inputs (Inputs. Keys (i))))
i + = 1
Loop
System.Web.HttpContext.Current.Response.Write ( "</ form>" )
System.Web.HttpContext.Current.Response.Write ( "</ body> </ html>" )
System.Web.HttpContext.Current.Response. End ()
End sub
End class

Form the request:
Dim myremotepost As RemotePost = New RemotePost ()
myremotepost.Add ( "out_summ" , txtPay.Text)
'how much the user gives
myremotepost.Add ( "mrh" , "mylogin" )
'id prodovtsa in robocase
Dim temp As Integer = pay.Add (txtPay. Text, Membership. GetUser.UserName, Now)
'pay class - means of interaction with the pay table
'(fields: pay - payment amount, user - name of the paying user, date - payment date,
'id - identifier, status - “OK”, when the payment was completed).
'Pay.add - fills in all fields, returns id.
myremotepost.Add ( "inv_id" , temp)
'payment id
myremotepost.Add ( "inv_desc" , "blah blah blah" )
'payment description, displayed in robokassa
myremotepost.Add ( "crc" , md5.MD5Hash ( "mylogin:" & txtPay.Text.Trim & ":" & temp & ": secret_1" ))
'MD5 checksum - a string representing a 32-bit number
'in hexadecimal form and in any case (32 characters total 0-9, AF).
'Generated by a string containing all required parameters,
'separated': ', with the addition of Password # 1
myremotepost.Url = " www.roboxchange.com/ssl/calc.asp "
'sent a request
myremotepost.Post ()

After the robokassa works, if everything is ok, then you will return a request to the “Success URL”.
We catch him:
Protected Sub Page_Load ( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Dim str As String = ""
Dim h As String = ""
Dim id As String = ""
If Not (Request.Form ( "out_summ" ) Is Nothing ) Then
str + = (Request.Form ( "out_summ" )) & ":"
End if
If Not (Request.Form ( "inv_id" ) Is Nothing ) Then
str + = (Request.Form ( "inv_id" )) & ":"
id = (Request.Form ( "inv_id" ))
End if
If Not (Request.Form ( "crc" ) Is Nothing ) Then
h + = (Request.Form ( "crc" ))
End if
str + = "secret_2"
'Password # 2
If md5.MD5Hash (str) = h Then
pay.ok (id)
'pay.ok - by id finds the payment, puts its status as “ok”
'and adds the payment amount to the user.
Response.Write ( "OK" & id)
'answered by the robokassa that the request was processed
End if
End sub

Actually, everything, the user has received money for his account, can spend =)
_________________________
Class md5
Public Class md5
Shared Function MD5Hash ( ByVal instr As String ) As String
Dim strHash As String = String .Empty

For Each b As Byte In New System.Security.Cryptography.MD5CryptoServiceProvider (). ComputeHash (Encoding. [ Default ] .GetBytes (instr))
strHash + = b.ToString ( "X2" )
Next
Return strHash
End function

Interface descriptions / Examples for both ASP.NET and other languages:
www.robokassa.ru/Doc/Ru/Interface.aspx
Beautiful brackets on the conscience of Source Code Highlighter

Source: https://habr.com/ru/post/39187/


All Articles