What's new
Carbonite

South Africa's Top Online Tech Classifieds!
Register a free account today to become a member! (No Under 18's)
Home of C.U.D.

PHP to dotnetCore help

Status
Not open for further replies

x3llo

Epic Member
Rating - 100%
29   0   0
Joined
Jul 12, 2014
Messages
347
Reaction score
100
Points
3,285
hey guys wondering if there are any developers here that can help me?

I'm currently stuck with a problem, i have an API and i need to post data to a payment gateway but they only have PHP examples and i'm struggling to convert it to .net core.. are there any developers that can help solve this for me?
i have all the needed data i just dont know how to create the post request within .net core my code so far =

StringBuilder builder = new StringBuilder();
builder.Append(@"<? xml version = ""1.0"" encoding = ""UTF-8"" ?>");
builder.Append("\n");
builder.Append("<Auth>");
builder.Append("\n");
builder.Append("<BatchReference>" + "Ref1" + "</BatchReference>");
builder.Append("\n");
builder.Append("<NotificationUrl>" + "mywebsite.com -&nbspmywebsite Resources and Information." + "</NotificationUrl>");
builder.Append("\n");
builder.Append("<BatchData>");
builder.Append("<BatchLine>" + Transactions[0].TRANSACTION_TYPE + ", " + Transactions[0].REFERENCE + ", " + "NetrecTest" + ", " + Transactions[0].VAUILT_ID + ", " + Transactions[0].BUDGET_PERIOD + ", " + Transactions[0].AMOUNT + "</BatchLine>");
builder.Append("\n");
builder.Append("</BatchData>");
builder.Append("\n");
builder.Append("</Auth>");

var content = new StringContent(builder.ToString(), Encoding.UTF8, "application/xml");

// this is required it gets the correct paygateId stored in the databse
string paygateId = _context.PaygateInfos.Select(s => s.Paygate_Id).FirstOrDefault();

// this is required it creates the authorisation header for
var byteArray = Encoding.ASCII.GetBytes(paygateId + ":" + "test");

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

var response = await client.PostAsync("https://secure.paygate.co.za/paybatch/1.2/process.trans", content);

var responseContent = await response.Content.ReadAsStringAsync();

var results = Dictionary.ToDictionary(responseContent);

how the request is suppose to look =

<Auth>
<BatchReference>batch_reference</BatchReference>
<NotificationUrl>mywebsite.com -&nbspmywebsite Resources and Information.</NotificationUrl>
<BatchData>
(BatchLine)
</BatchData>
</Auth>


the php code is =

<?php

$batchReference = 'AuthBatch201800101';
$notificationUrl = 'http://yourserver.co.za/notifylocation/';

ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache

try {
//use SimpleXmlElement for better control of children
$xml = new SimpleXMLElement('<Auth />');

$xml->addChild('BatchReference', $batchReference);
$xml->addChild('NotificationUrl', $notificationUrl);

$batchData = $xml->addChild('BatchData');

$batchData->addChild('BatchLine', 'A, test1, PayGate Test, 18566ddc-b13b-4c9f-9e88-9a2f9a9dd64a, 00, 100');

//use DomDocument to remove XML headers
$dom = new DOMDocument();
$dom->loadXML($xml->asXML());

$soap = $dom->saveXML($dom->documentElement);

//Remove Auth tag because we pass it in the __soapCall
$childrenOnly = str_replace(['<Auth>', '</Auth>'], '', $soap);

} catch(Exception $e){
error_log($e->getMessage());
}

// array containing login and password for basic http auth, along with trace option for debugging
$options = array(
'login' => '10011072130', //Your PayGate ID
'password' => 'test', //Your encryption key (set in the PayXML configuration section of the PayGate Back Office)
'trace' => 1
);

try {
$soapClient = new SoapClient("https://secure.paygate.co.za/PayBatch/1.2/PayBatch.wsdl", $options); //point to WSDL and set options
$soapClient->__setLocation("https://secure.paygate.co.za/PayBatch/1.2/process.trans"); //actual location data is processed

$result = $soapClient->__soapCall('Auth', array(
new SoapVar($childrenOnly, XSD_ANYXML)
));

// $result is an object of AuthResponse
var_dump($result);

} catch(SoapFault $sf){
var_dump($sf->getMessage());
} catch(Exception $e){
error_log($e->getMessage());
}


the reference documentation = DPO PayGate Documentation
its the PayBatch section..

Any help will be much appreciated!!!

Thanks.
 
They have sample code for .NET: PayGate/sample-code-csharp

Which will be much simpler to convert to .net core...
Hi valiente

Thanks for the feedback, unfortunately that sample code is only for PayWeb and PayHost, two different packages, which i have successfully implemented but im struggling with the Paybatch Implementation.
 
What's wrong with your code as it is?

Two tools I can recommend for any api stuff is Telerik Fiddler (Fiddler - Free Web Debugging Proxy - Telerik) and Postman (Postman).

Fiddler will let you see the actual request being sent by your code, or any other code so if there's something that is working and something that isn't you can compare the actual requests in Fiddler and get more of an idea what the difference is.
Postman is more handy for constructing requests "by hand" but also if you're actually making the api you can use it to submit a request so you can debug etc. It's normally easier to use and "save" a request you might have gotten out of Fiddler than using the built in Fiddler "replay" function.

Because the api you're querying is HTTPS you'll have to enable fiddler to decrypt HTTPS requests so you can see what's in them. You'll have to install the fiddler cert on your pc so your pc trusts the fiddler proxy running on it. All of this is basically guided in the installation and the docs though. Just be aware otherwise you'll just see "tunnel to secure.paygate.co.za" in fiddler.
 
What's wrong with your code as it is?

Two tools I can recommend for any api stuff is Telerik Fiddler (Fiddler - Free Web Debugging Proxy - Telerik) and Postman (Postman).

Fiddler will let you see the actual request being sent by your code, or any other code so if there's something that is working and something that isn't you can compare the actual requests in Fiddler and get more of an idea what the difference is.
Postman is more handy for constructing requests "by hand" but also if you're actually making the api you can use it to submit a request so you can debug etc. It's normally easier to use and "save" a request you might have gotten out of Fiddler than using the built in Fiddler "replay" function.

Because the api you're querying is HTTPS you'll have to enable fiddler to decrypt HTTPS requests so you can see what's in them. You'll have to install the fiddler cert on your pc so your pc trusts the fiddler proxy running on it. All of this is basically guided in the installation and the docs though. Just be aware otherwise you'll just see "tunnel to secure.paygate.co.za" in fiddler.

So when using the string builder to build the xml request - if i do this -

StringBuilder builder = new StringBuilder();
builder.Append("<Auth>");
builder.Append("\n");
builder.Append("<BatchReference>" + "Ref1" + "</BatchReference>");
builder.Append("\n");
builder.Append("<NotificationUrl>" + "mywebsite.com -&nbspmywebsite Resources and Information." + "</NotificationUrl>");
builder.Append("\n");
builder.Append("<BatchData>");
builder.Append("<BatchLine>" + Transactions[0].TRANSACTION_TYPE + ", " + Transactions[0].REFERENCE + ", " + "NetrecTest" + ", " + Transactions[0].VAUILT_ID + ", " + Transactions[0].BUDGET_PERIOD + ", " + Transactions[0].AMOUNT + "</BatchLine>");
builder.Append("\n");
builder.Append("</BatchData>");
builder.Append("\n");
builder.Append("</Auth>");


-- Note that i removed the -

builder.Append(@"<? xml version = ""1.0"" encoding = ""UTF-8"" ?>");
builder.Append("\n");


-- Then I get this error response from the post method -

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:VersionMismatch</faultcode><faultstring>Wrong Version</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

-- if i add the removed line back i then get this error message--

builder.Append(@"<? xml version = ""1.0"" encoding = ""UTF-8"" ?>");


<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode><faultstring>Bad Request</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>


i feel the error is coming form

soap.wsdl_cache_enabled

but .net core does not have any options to disable or enable the soap.wsdl_cache as its mentioned in the php code?
 
Status
Not open for further replies

Users who are viewing this thread

Back
Top Bottom