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.