Get order transaction ID from authorize.net Authorize AIM - php

I'm making a custom woocommerce reports plugin that will display certain information and spit it out as a .csv. I have it return stuff like name, company name, product, and amount. I do this the following way.
/**
* Check if we need customer phone.
*/
case 'wc_settings_tab_customer_phone':
array_push( $csv_values, self::customer_meta( get_the_ID(), '_billing_phone' ) );
break;
Now I'm using the Authorize.net AIM Payment Gateway for Woocommerce plugin so a Transaction ID is generated.
I want to include this in my .csv export. How would I go about doing this? I tried looking in the plugin files and noticed this is what the transaction id was $response_array[6] but can't figure out how to return it. If someone could show me how to tap into Authorize.net API and get the transaction ID that would be awesome! Thanks in advanced!
EDIT: Here is what I've got so far. I added the php code to connect but can't seem to pull the order transaction id. By the way, the "x_login" and "x_tran_key" have been taken out and replaced with "test123".
case 'wc_settings_tab_authorize_id':
$post_url = "https://secure.authorize.net/gateway/transact.dll";
$post_values = array(
// the API Login ID and Transaction Key must be replaced with valid values
"x_login" => "test123",
"x_tran_key" => "test123",
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// This sample code uses the CURL library for php to establish a connection,
// submit the post, and record the response.
// If you receive an error, you may want to ensure that you have the curl
// library enabled in your php configuration
$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object
// This line takes the response and breaks it into an array using the specified delimiting character
$response_array = explode($post_values["x_delim_char"],$post_response);
array_push( $csv_values, TransactionID() );
break;
EDIT 2: Implementing John's code
case 'wc_settings_tab_authorize_id':
$post_url = "https://secure.authorize.net/gateway/transact.dll";
$post_values = array(
// the API Login ID and Transaction Key must be replaced with valid values
"x_login" => "test123",
"x_tran_key" => "test123",
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// This sample code uses the CURL library for php to establish a connection,
// submit the post, and record the response.
// If you receive an error, you may want to ensure that you have the curl
// library enabled in your php configuration
$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object
$post_response = '1|1|1|This transaction has been approved.|4DHVNH|Y|2230582188|none|Test transaction for ValidateCustomerPaymentProfile.|0.00|CC|auth_only|none|John|Doe||123 Main St.|Bellevue|WA|98004|USA|800-555-1234|800-555-1234|email#example.com|||||||||0.00|0.00|0.00|FALSE|none|E440D094322A0D406E01EDF9CE871A4F||2|||||||||||XXXX1111|Visa||||||||||||||||';
$response_array = explode('|',$post_response);
$transaction_id = $response_array[6];
array_push( $csv_values, $transaction_id );
break;
EDIT 3: Okay, this is what I currently have (excluding the api and transaction key).
/**
* Check for authorize.net transaction id.
*/
case 'wc_settings_tab_authorize_id':
$post_url = "https://secure.authorize.net/gateway/transact.dll";
$post_values = array(
// the API Login ID and Transaction Key must be replaced with valid values
"x_login" => "TEST",
"x_tran_key" => "TEST",
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// This sample code uses the CURL library for php to establish a connection,
// submit the post, and record the response.
// If you receive an error, you may want to ensure that you have the curl
// library enabled in your php configuration
$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, http_build_query($post_values)); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object
// This line takes the response and breaks it into an array using the specified delimiting character
$response_array = explode($post_values["x_delim_char"],$post_response);
$transaction_id = $response_array[6];
array_push( $csv_values, $transaction_id );
break;
Still can't figure out why this won't work. When I try and return the $transaction_id I get the value 0. When I try $post_response to see what it returns I get this:
3|2|33|Credit card number is required.||P|0|||0.00|CC|auth_capture||||||||||||||||||||||||||THISISANALPHANUMERICNUMBER||||||||||||||||||||||||||||||
Before there was an alphanumberic string but I replaced it for security purposes. Do you think this may be happening because I'm not setting a cc number or billing address with it?

The response string returned by Authorize.Net is going to look something like this:
1|1|1|This transaction has been approved.|4DHVNH|Y|2230582188|none|Test transaction for ValidateCustomerPaymentProfile.|0.00|CC|auth_only|none|John|Doe||123 Main St.|Bellevue|WA|98004|USA|800-555-1234|800-555-1234|email#example.com|||||||||0.00|0.00|0.00|FALSE|none|E440D094322A0D406E01EDF9CE871A4F||2|||||||||||XXXX1111|Visa||||||||||||||||
This is the results separated by a | which is the delimiting character you set here:
"x_delim_char" => "|",
You correctly break apart the string using explode():
$response_array = explode($post_values["x_delim_char"],$post_response);
which gives us that data in an array called $response_array.
In Authorize.Net's response, the transaction ID is 2230582188. In our array that is the seventh element so we can get it using:
$transaction_id = $response_array[6];
Here is a demo showing you this works.

Related

how to send and receive tokens via Solana JSONRPC

there's the following ETH JSONRPC implementation to transfer tokens, which can be utilised via curl in PHP and I would like to do EXACTLY the same but on the Solana Blockchain - which supports it's own JSONRPC implementation
var whatever= {};
whatever.jsonrpc="2.0";
whatever.id=1;
whatever.method="eth_sendTransaction";
whatever.params= [];
whatever.params[0].from="0x52f273a06a420453aa5b33c4f175395c9a1fddd8";
whatever.params[0].to=data.ethAddress;
whatever.params[0].value=1e18;
whatever.params[0].currency="xxx";
source on stack overflow for the above code is here
as the Solana Documentation mentions only sendTrasactions here are my two questions:
how to implement the above example using Solana (see below our curl implementation of the Solana JSONRPC to get a user's current token amount in php i.e. "getTokenAmount")
where do I get the fully-signed Transaction as an encoded string which is a parameter for the "sendTransaction"*
*I assume the fully-signed Transaction is issued once the transfer is made, no?
----- example of our implementation as mentioned above (for those who might be interested in it -----
public function getTokenAmount($wallet_address, $token_address)
{
$data = array(
"jsonrpc" => "2.0",
"id" => 1,
"method" => "getTokenAccountsByOwner",
"params" => array(
0 => $wallet_address,
1 => array(
"mint" => $token_address
),
2 => array(
"encoding" => "jsonParsed"
)
)
);
$data = json_encode($customer);
$response = $this->initCurl($data);
return $response->result->value[0]->account->data->parsed->info->tokenAmount->uiAmount;
}
private function initCurl($data)
{
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
#curl_setopt($ch, CURLOPT_URL, $this->_endpoint);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array( "accept: application/json", "content-type: application/json"));
$response = json_decode(#curl_exec($ch));
$err = #curl_error($ch);
curl_close($ch);
// TODO: ERROR HANDLING
if ($err) {
return false; //"cURL Error #:" . $err;
}
return $response;
}
The answers to 1 and 2 are very similar.
In order to send a transaction, you must create a base64 or base58 encoded string with all of the transaction data, which includes accounts, data, and any other flags needed. After that, you must sign the transaction with a ed25519 key, and provide that whole string as the transaction.
You can build this yourself by following how it's done for JS:
system instruction, specifically transfer: https://github.com/solana-labs/solana/blob/005592998dd107b3d54d9203babe24da681834f5/web3.js/src/system-program.ts#L676
transaction, specifically adding instructions, compiling the message, and signing: https://github.com/solana-labs/solana/blob/005592998dd107b3d54d9203babe24da681834f5/web3.js/src/transaction.ts#L205
Your better bet, however, would be to reuse an existing library which has already done a lot of this work for you. For example, https://github.com/tighten/solana-php-sdk#transactions provides an easy API to do exactly what you're looking for.

Unable to upload file on Teamwork using API in PHP

I am uploading a file on teamworking using the following link
https://developer.teamwork.com/projects/questions/fileUpload-preferred
Here I am using PHP Codeigniter 4.x framework with CURL to send request and in response to the first step, I am getting Ref and URL both values. i.e. First step is working fine on my end.
Now for the second step as mentioned in the documentation as follows,
Send a PUT request to the link above, with the ‘file’ in the body of the request. Along with this, you need the following headers:
X-Amz-Acl: public-read,
Content-Length: file-size
Host:host-from-the-generate-link
I am passing file object
CodeIgniter\HTTP\Files\UploadedFile Object ( [path:protected] =>
/var/www/web116/tmp/phpDyYgjj [originalName:protected] =>
banner-01.png [name:protected] => banner-01.png
[originalMimeType:protected] => image/png [error:protected] => 0
[hasMoved:protected] => [size:protected] => 639542
[pathName:SplFileInfo:private] => /var/www/web116/tmp/phpDyYgjj
[fileName:SplFileInfo:private] => phpDyYgjj )
in request body.
Here is my code as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // $url is value which I have gotten after first step
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Amz-Acl: public-read',
'Content-Length:' . $fileSize, // size of file
'Host: tw-bucket.s3.amazonaws.com'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['file' => $file])); // $file is file object which I have printed above
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
$result = (array) json_decode($json);
$result['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
But in the HTTP code, I am getting 403 rather than 200.
Can anyone please help me out with this so that I can upload the file on teamwork? I am working to upload a file for a particular task on teamwork.
Thanks
You can find another API to bypass this issue until the Teamwork API Support Team can fix this issue, here is what I did to make this work:
Step 1: Use the API https://apidocs.teamwork.com/docs/teamwork/86ecebd6161af-file-uploading-via-the-api-classic
To do this, you make an API Call to POST /pendingFiles.json. The actual file contents are sent via a form field called “file.”
If this is successful, you will receive a Status Code of 201 (Created) and a structure containing a reference code.
{ “pendingFile”: { “ref” : “tf_xxxxxxxxxxxxxxxx” } }
The ref bit is the important part and is the Pending File Handle.
Step 2: Create an actual file object using the Pending File Handle
The next step is to create the actual file object in a project on your Teamwork account.
To do this, you make an API Call to POST /projects/{id}/files.json where {id} is the project you want to create the file.
{
“file”: {
“description”: “Optional string describing the file”,
“category-id”: “ID of the category you to create the file in. Pass 0 if no category”,
“category-name”: “String if you want to create a new category – Pass category-id=0 also”,
“pendingFileRef”: “tf_xxxxxxxxxxxxxxxx”
}
}
Please note the "fileId" instead of the "pendingFileRef" if you want to attach the file to a task.
This is not a pending file like the other API.

How do I send info to this API using cURL and PUT?

I am working with an API that is documented here: https://cutt.ly/BygHsPV
The documentation is a bit thin, but I am trying to understand it the best I can. There will not be a developer from the creator of the API available before the middle of next week, and I was hoping to get stuff done before that.
Basically what I am trying to do is update the consent of the customer. As far as I can understand from the documentation under API -> Customer I need to send info through PUT to /customers/{customerId}. That object has an array called "communicationChoices".
Going into Objects -> CustomerUpdate I find "communicationChoices" which is specified as "Type: list of CommunicationChoiceRequest". That object looks like this:
{
"choice": true,
"typeCode": ""
}
Doing my best do understand this, I have made this function:
function update_customer_consent() {
global $userPhone, $username, $password;
// Use phone number to get correct user
$url = 'https://apiurlredacted.com/api/v1/customers/' . $userPhone .'?customeridtype=MOBILE';
// Initiate cURL.
$ch = curl_init( $url );
// Specify the username and password using the CURLOPT_USERPWD option.
curl_setopt( $ch, CURLOPT_USERPWD, $username . ":" . $password );
// Tell cURL to return the output as a string instead
// of dumping it to the browser.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// Data to send
$data = [
"communicationChoices" => [
"communicationChoiceRequest" => [
"choice" => true,
"typeCode" => "SMS"
]
]
];
$json_payload = json_encode($data);
print_r($json_payload);
// Set other options
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json_payload)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload);
// Execute the cURL request
$response = curl_exec($ch);
// Check for errors.
if( curl_errno( $ch ) ) :
// If an error occured, throw an Exception.
throw new Exception( curl_error( $ch ) );
endif;
if (!$response)
{
return false;
} else {
// Decode JSON
$obj = json_decode( $response );
}
print_r($response);
}
I understand that this is very hard to debug without knowing what is going on within the API and with limited documentation, but I figured asking here was worth a shot anyway.
Basically, $json_payload seems to be a perfectly fine JSON object. The response from the API however, is an error code that means unknown error. So I must be doing something wrong. Maybe someone has more experience with APIs and such documentation and can see what I should really be sending and how.
Any help or guidance will be highly appreciated!
before you test your code, you can use the form provided on the API Documentation.
when you navigate to API > Customers > /customers/{customerId} (GET), you will see a form on the right side of the page (scroll up). you need to provide the required values on the form then hit Submit button. you will surely get a valid data for communicationChoices based on the result from the Response Text section below the Submit button.
now, follow the data structure of communicationChoices object that you get from the result and try the same on API > Customers > /customers/{customerId} (PUT) form.
using the API forms, you may be able to instantly see a success or error from your input (data structure), then translate it to your code.

cURL using info from mySQL, then storing the cURL'ed info

I'm programming in PHP.
An article I've found useful until now was mainly about how to CURL through one site with a lot of information, but what I really need is how to cURL on multiple sites with not so much information - a few lines, as a matter of fact!
Another part is, the article focus is mainly at storing it at the FTP server in a txt file, but I have loaded around 900 addresses into mysql, and want to load them from there, and enrich the table with the information stored in the links - Which I will provided beneath!
We have some open public libraries with addresses and information about these and an API.
Link to the main site:
The function I would like to use: http://dawa.aws.dk/adresser/autocomplete?q=
SQL Structure:
Data example: http://i.imgur.com/jP1J26U.jpg
fx this addresse: Dornen 2 6715 Esbjerg N (called AdrName in databasen).
http://dawa.aws.dk/adresser/autocomplete?q=Dornen%202%206715%20Esbjerg%20N
This will give me the following output (which I want to store in the AdrID in the database):
[
{
"tekst": "Dornen 2, Tarp, 6715 Esbjerg N",
"adresse": {
"id": "0a3f50b8-d085-32b8-e044-0003ba298018",
"href": "http://dawa.aws.dk/adresser/0a3f50b8-d085-32b8-e044-0003ba298018",
"vejnavn": "Dornen",
"husnr": "2",
"etage": null,
"dør": null,
"supplerendebynavn": "Tarp",
"postnr": "6715",
"postnrnavn": "Esbjerg N"
}
}
]
How to store it all in a blob, as seen in the SQL structure?
If you want to make a cURL request in php use this method
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
And then you call it using
print curl_download('http://dawa.aws.dk/adresser/autocomplete?q=Melvej');
Or you can directly convert it jSON object
$jsonString=curl_download('http://dawa.aws.dk/adresser/autocomplete?q=Melvej');
var_dump(json_decode($jsonString));
The data you download is json, so you can store that in a varchar column rather than blog.
Also the site with the api does not seem bothered about http referrer, user agent etc so you can use file_get_contents in place of curl.
So simply get all the results from your db, iterate over them, making a call to the api, and update the appropriate row with the correct data:
//get all the rows from your database
$addresses = DB::exec('SELECT * FROM addresses'); //i dont know how you actually access your db, this is just an example
foreach($addresses as $address){
$searchTerm = $address['AdrName'];
$addressId = $address['Vid'];
//download the json
$apidata = file_get_contents('http://dawa.aws.dk/adresser/autocomplete?q=' . urlencode($searchTerm));
//save back to db
DB::exec('UPDATE addresses SET status=? WHERE id=?', [$apidata, $searchTerm]);
//if you want to access the data, you can use json_decode:
$data = json_decode($apidata);
echo $data[0]->tekst; //outputs Dornen 2, Tarp, 6715 Esbjerg N
}

Callback from API not happening after posting parameters to API URL from server side

API integration description
The API needs a form to be posted to the API URL with some input fields and a customer token. The API processes and then posts response to a callback.php file on my server. I can access the posted vals using $_POST in that file. That's all about the existing method and it works fine.
Requirement
To hide the customer token value from being seen from client side. So I started with sending server side post request.
Problem
I tried with many options but the callback is not happening -
1) CURL method
$ch = curl_init(API_URL);
$encoded = '';
$_postArray['customer_token'] = API_CUSTOMER_TOKEN;
foreach($_postArray as $name => $value)
{
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
$resp echoes 1 if the line curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); is removed but the callback does not happen. I am setting a session variable in the callback script to verify.Is it needed that the API be synchronous in order to use curl method, so that curl_exec returns the response?
2) without CURL as given in Posting parameters to a url using the POST method without using a form
But the callback is not happening.
I tried with the following code too, but looks like my pecl is not installed properly because the HttpRequest() is not defined.
$req = new HttpRequest($apiUrl, HttpRequest::METH_POST);
$req->addQueryData($params);
try
{
$r->send();
if ($r->getResponseCode() == 200)
{
echo "success";
// success!
}
else
{
echo "failure";
// got to the API, the API returned perhaps a RESTful response code like 404
}
}
catch (HttpException $ex)
{
// couldn't get to the API (probably)
}
Please help me out! I just need to easily send a server side post request and get the response in the callback file.
Try to debug your request using the curl_get_info() function:
$header = curl_getinfo($ch);
print_r($header);
Your request might be OK but it my result in an error 404.
EDIT: If you want to perform a post request, add this to your code:
curl_setopt($ch, CURLOPT_POST, true);
EDIT: Something else I mentioned at your code: You used a '1' at the 'CURLOPT_RETURNTRANSFER' but is should be 'true':
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
At least this is how I usually do it, and you never know if the function will also understand a '1' as 'true';
EDIT: The real problem: I copy-pasted your source and used it on one of my pages getting this error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\phptests\test.php on line 8
The error is in this line:
foreach($_postArray as $name => $value)
$_postArray is an array with one value holding the other values and you need either another foreach or you simple use this:
foreach($_postArray['customer_token'] as $name => $value)
As discussed in the previous question, the callback is an entirely separate thing from your request. The callback also will not have your session variables, because the remote API is acting as the client to the callback script and has its own session.
You should really show some API documentation here. Maybe we're misunderstanding each other but as far as I can see, what you are trying to do (get the callback value in the initial CURL request) is futile, and doesn't become any less futile by asking twice.

Categories