Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I would like to convert php curl code below to asp.net.

<?php require_once("config.php");
  
if($_POST['token']!='' and isset($_POST['token']) ){

$token = $_POST['token']; 

$url = 'https://dev-kpaymentgateway-services.kasikornbank.com/card/v2/charge'; 
$datasend = array(
   "amount"=> "2000.00",   
   "currency"=> "THB",  
   "description" => "Awesome Product",
   "source_type" => "card",    
   "mode"=> "token",   
   "token"=> $token,
   "reference_order"=> "11251513" 
);
      
$ch = curl_init();
$post_string = json_encode($datasend);  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
	'Content-Type: application/json',              
	'Cache-Control:no-cache',
  'x-api-key: '.$secretkey 
  )                                                                       
);
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_SSLVERSION, 0 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$data = curl_exec($ch);
$response = json_decode($data);

curl_close ($ch);

$response = json_decode(json_encode($response), True);

ob_clean();
header('Location: '.$response['redirect_url']);
     }
?> 


What I have tried:

So , i've tried convert to asp.net with code below.

Dim AppKey As String = "skey_test_xxxxxxxxxx"   
        Dim URL As String = "https://dev-kpaymentgateway-services.kasikornbank.com/card/v2/charge/"
        Try
            ServicePointManager.Expect100Continue = True
            ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
            Dim CjSONString As String
            Dim li As jsonGetToken = New jsonGetToken()
            li.amount = 2000.0
            li.currency = "THB"
            li.description = "Awesome Product"
            li.source_type = "card"
            li.mode = "token"
            li.reference_order = "11251513"
            CjSONString = "{""amount"": " & li.amount & ",""currency"": " & """" & li.currency & """" & ",""description"": " & """" & li.description & """" & ",""source_type"": " & """" & li.source_type & """" & ",""mode"": " & """" & li.mode & """" & ",""reference_order"": " & """" & li.reference_order & """" & "}"

            Dim URLHTTP As String = URL
            Dim StringJSON As jsonGetToken = JsonConvert.DeserializeObject(Of jsonGetToken)(CjSONString)
            Dim jSONString As String = JsonConvert.SerializeObject(li)

            Me.txtja.Text = jSONString
            
            Dim request = CType(WebRequest.Create(URLHTTP), HttpWebRequest)
            request.ContentType = "application/json; charset=UTF-8"
            request.Method = "POST"
            request.ContentLength = jSONString.Length
            request.Headers.Add("Authorization", "Basic " + AppKey)

            Dim output As String = ""
            Using streamWriter = New StreamWriter(request.GetRequestStream())
                streamWriter.Write(jSONString)
                streamWriter.Flush()
                streamWriter.Close()
                Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)
                Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                    output = streamReader.ReadToEnd()
                End Using
            End Using
            
            Me.txtAert.Text = output

        Catch ex As Exception
            Me.txtAert.Text = ex.Message
        End Try


But that return error.
The remote server returned an error: (401) Unauthorized.


In part data, i've sent json format as below.
{"amount":2000.0,"currency":"THB","description":"Awesome Product","source_type":"card","mode":"token","reference_order":"11251513"}



please suggest me. Thank you.
Posted
Updated 13-Sep-22 16:28pm
v3
Comments
F-ES Sitecore 21-Aug-20 6:32am    
The code looks a little over-complicated, but one thing I see missing is that the original sets the x-api-key request header and your code does not. You're also using basic auth where the original doesn't. Use a tool like Fiddler or wireshark to compare the two requests to see how they differ.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900