How to send data back to woocommerce custom gateway after processing - wordpress

I am trying to get response data back to my payment gateway with no success.
This is the code snippet within my woocommerce extension that sends data to my payment processor(index.php)
$environment_url = "https://example.com/api/index.php";
$payload = array(
// Credentials and API Info
"x_tran_key" => $this->trans_key,
"x_login" => $this->api_login,
"x_version" => "3.1",
// Order total
"x_amount" => $customer_order->order_total
);
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
I get the data very fine to the url(index.php). The issue is getting the response back to my custom woocommerce extension so that I can complete payment_process()
Most resources I have found online only talk about sending data, but not how the response or result is sent back to the querying resource file.
Any help will be highly appreciated. Thank you.

Just did a simple print_r and it did the trick, I got my results in an array and used print_r
If anybody encounters the same problem as I did, I hope this has saved your time.

Related

How to connect WordPress / WooCommerce to a REST API with PHP?

I am trying to write my first REST API with PHP. We want to send shipping data to the shipping company and get labels for printing in return. The shipping company has given me test api credentials.
So I started to write a plugin with this code inside:
$url = "https://mywebsite/endpoint/";
$username = "myusername";
$password = "mypassword";
$data = array(
'key1' => 'value1',
'key2' => 'value2'
);
$response = wp_remote_post( $url, array(
'body' => $data,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
)
);
var_dump($response); // not being called
When I run this code, I get a white screen with no error message, the last line with var_dump($response) is not being called. I am a bit stuck here because I se no success and no error...what could it be? Why this behaviour of the remote server? Does it mean "sorry, wrong credentials" or "data has the wrong format"? I have the feeling that the server doesn't even notice that I'm trying to contact him...
I tried a number of other variatons of the above code that I found somwehere online, and also outside of WordPress, but no success.
The shipping company gave me a documentation. It says:
HTTP Method: POST
Authentication Header: Basic Authentication (user name/ password)
so I thought I could do no wrong when trying that. Hm.
I found a solution, so I can share it here with you.
I installed the free Software Postman and created a simple POST query there. After entering $url, $username and $password I ran a query and it worked well. the good thing abort Postman is that it creates code snippets for you. "very_long_cryptic_string" is obviously created from $username and $password. The PHP for my case goes like this, and it works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic very_long_cryptic_string'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Resolved: Wordpress API Post request not updating

I'm tring to write a WordPress post request (API) and although it reports a success message, the data is not updated. I temporarily created two extra columns in the WordPress user table (yes, not best practice). But the data is not saved. The jwt-auth plugin is used.
Current rest route:
register_rest_route( 'jwt-auth/v1', 'base_coords_post', array(
'methods' => 'POST',
'callback' => 'base_coords_post',
));
And the callback
function base_coords_post() {
global $wpdb;
$userId = get_current_user_id();
$data = array(
'lat' => $_POST['lat'],
'lng' => $_POST['lng']
);
$data_where = array(
'ID' => $userId
);
$wpdb->update($wpdb->users , $data, $data_where);
}
How is it that the columns are not being updated? I checked the post values, they exist, the userID also exists.
EDIT
The fix was i tried to add varchar longer than the database field can hold. Changing the DB fixed the issue.

Handle incoming Array from Guzzle

So i have been trying to get guzzle working, i can send a post request which sends some kind of array and I would like to know, how can i receive it at an endpoint? i can handle things like if(isset($_POST['nameHere'])) but not the array thingy
$client = new Client();
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'email' => 'test#gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
'debug' => true
]);
echo '<pre>' . print_r((string)$response->getBody(), true) . '</pre>';
}
ps: My custom laravel application is the sender, my wordpress site is the receiver.
Got it working, i was missing the .php ending in my url, because i chose not to test with httpbin but directly with my api.
and in wordpress i dont get anything because this is the sendercode i published not the receiver (wordpress). i wouldnt know how to test it. thanks anyway

Update User Meta with spaces using REST API - WordPress

I'm trying to update the user meta with spaces registered on my website. I am using register_meta hook to register user meta. And for updating users using WordPress REST API I use wp_remote_post but the problem is when I tried to update a user meta with spaces it will add as array in response then it will add as new user meta.
For example the value of user meta is - community test
Sample Response:
[meta] => Array
(
[agent_community] => Array
(
[0] => community
[1] => test
)
)
Sample Code for registering user meta.
register_meta('user', 'agent_community', array(
"type" => "string",
"show_in_rest" => true
));
Update using wp_remote_post
$update_data = array('email'=> 'test#mail.com', 'meta[agent_community]' => 'community test');
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
),
'body' => array_filter($update_data));
$update_url = 'https://test-url.com/wp-json/wp/v2/users/17';
$response = wp_remote_post( $update_url, $args);
Database:
Is there a way that can merge the array on user meta?
implode() might help you. And you can change spaces with HTML entities. Or use function like urledncode() / urlendecode() or something like this.

Facebook Posting using FB API phpSDK

i created my app on fb with permissions ready to publish on users behalf, the thing is, a regular post has Like and Comment links, like buttons on bottom of the post, i want to add my custom link : VOTE NOW, its a poll post
how can i do that?
someone gave an answer but for js sdk not php, n i cant find it on facebook dev documentation
some gave a close enough solution, but ddnt seem to work on php with modifications
FB.ui({
method: "feed",
link: "LINK_URL",
...
actions: [
{ name: "Read Now", link: "URL TO THE READ NOW " }
]
}, function(response) { console.log(response); });
It seems it's working with /me/feed but with my custom /me/xxxxxx:submitted_a_poll/ its not working
When you make a call with the PHP SDK, you also pass a similar set of parameters:
$attachment = array(
'link' => 'http://your-cool-site.com',
'description' => 'This is the description',
...
'actions' => array(
array(
'name' => 'Vote Now!',
'link' => 'http://your-cool-site.com/vote.php'
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);
All you really have to do is add the relevant action settings to your parameters.

Resources