How to take only payment on another woocomerce website - wordpress

I want to redirect my buyers to my second website 'B' to make payment for my product on website 'A' and after successful payment on website 'B', it should automatically update the order status on website 'A' as the order is completed. I am using woo commerce plugin for my websites. Please any plugins, Add-ons, or custom code suggestions. please I need help
Site 'A'
function action_woocommerce_new_order( $order_id ) {
//get all order details by $order_id
//post your data to site B
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://yoursiteB.com/wp-json/api/v2/create_wc_order");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
};
add_action( 'woocommerce_payment_complete', 'action_woocommerce_new_order', 10, 1 );
Site 'B'
Class WC_Rest_API {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'init_rest_api') );
}
public function init_rest_api() {
register_rest_route('api/v2', '/create_wc_order', array(
'methods' => 'POST',
'callback' => array($this,'create_wc_order'),
));
}
public function create_wc_order( $data ) {
//$data - all the details needs to be send from site A
global $woocommerce;
$user_id = $data['user_id'];
$user = get_user_by( 'ID', $user_id);
$phonenumer = get_user_meta( $user_id,'user_phoneno', true);
if($user) {
$address = array(
'first_name' => $user->display_name,
'last_name' => $user->display_name,
'email' => $user->user_email,
'phone' => $phonenumer,
);
$order = wc_create_order();
//get product details from rest api site A
$order->add_product();
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order = wc_get_order( $order->id );
$response['status'] = 'success';
$response['message'] = 'Order created on wordpress site B.';
return new WP_REST_Response($response, 200);
}
}
}
$rest_api = new WC_Rest_API();
this doesn't work as it create product on the second website.
I want to use the second website to make only payment if it can pull user data and order id only

Related

How speed up the woocommerce REST API in sync product stock quantity?

I want sync stock quantity of site B (remote site) with site A, ie. when an order in site A is in process status, the quantity of same product in site B will be updated.
For mapping product IDs, I add post meta in both site A and B.
In order to do so, I use following codes. It works, but my problem is speed process when stock quantity wants to be updated in site B, it takes more seconds.
add_action('woocommerce_order_status_changed', 'update_base_site_inventory',11, 1);
function update_base_site_inventory($order_id){
$order = wc_get_order( $order_id );
if ($order->status == 'processing') {
$items = $order->get_items();
foreach($items as $item){
$product = wc_get_product($item->get_product_id());
$product_quantity = $product->get_stock_quantity();
$id_mapped = get_post_meta($item->get_product_id(), 'map_product_id', true);
$batch_update['update'] = array(array('id' => current($id_mapped), 'stock_quantity' => $product_quantity));
}
$api_response = wp_remote_request(
'https://...../wp-json/wc/v3/products/batch/', array(
'method' => 'POST',
'headers' => array('Authorization' => 'Basic ' . base64_encode( '....:.....' )),
'body' => $batch_update
)
);
}
}
is my approach correct? what should I do? is there any way to increase the API request speed?
Thanks,
Try using WooCommerce Client Library, install using composer:
composer require automattic/woocommerce
Then use it in your code like this:
add_action('woocommerce_order_status_changed', 'update_base_site_inventory', 11, 1);
function update_base_site_inventory($order_id)
{
$order = wc_get_order($order_id);
if ($order->status == 'processing') {
$data = array();
$items = $order->get_items();
foreach ($items as $item) {
$product = wc_get_product($item->get_product_id());
$product_quantity = $product->get_stock_quantity();
$id_mapped = get_post_meta($item->get_product_id(), 'map_product_id', true);
$data[] = array(
'id' => current($id_mapped),
'stock_quantity' => $product_quantity
);
}
// Include the library
require __DIR__ . '/vendor/autoload.php';
// Initialize the library
$woocommerce = new Automattic\WooCommerce\Client(
'https://example.com', // Your store URL
'ck_....................', // Your consumer key
'cs_....................', // Your consumer secret
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v3', // WooCommerce WP REST API version
'verify_ssl' => false
]
);
// Make the update
$woocommerce->post('products/batch', array(
'update' => array($data),
));
}
}

Wordpress does not send a POST request with data

I need to export woocommerce order data using a POST request to a third-party url.I have this code in functions.php
add_action('woocommerce_thankyou', 'wdm_send_order_to_ext');
function wdm_send_order_to_ext ( $order_id ){
// get order object and order details
$order = new WC_Order( $order_id );
$email = $order->get_billing_email();
$phone = $order->get_billing_phone();
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();
// set the address fields
$user_id = $order->get_user_id();
$address_fields = array('country',
'title',
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'address_3',
'address_4',
'city',
'state',
'postcode');
$address = array();
if(is_array($address_fields)){
foreach($address_fields as $field){
$address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
$address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
}
}
// set the username and password
$api_username = 'username';
$api_password = 'password';
// setup the data which has to be sent
$data = array(
'username' => $api_username,
'password' => $api_password,
'name1' => $address['billing_first_name'],
'street' => $address['billing_address_1'],
'city' => $address['billing_city'],
'country' => $address['billing_country'],
'pcode' => $address['billing_postcode'],
'phone' => $phone,
'weight' => 4.0,
'parcel_type' => 'D',
'num_of_parcel' => 1
);
// send API request via cURL
$curl = curl_init();
/* set the complete URL, to process the order on the external system. Let’s consider http://example.com/buyitem.php is the URL, which invokes the API */
curl_setopt($curl, CURLOPT_URL, "https://easyship.si/api/parcel/parcel_import");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($curl);
curl_close ($curl);
}
However, it does not pass the request, the third-party API does not receive anything. WP_DEBUG doesn't see any errors. Tell me where to look, what is wrong?
You can use https://requestbin.com/ as endpoint to see data you are posting and if they are being posted correctly. If it successfully posting it to requestbin endpoint you can see the header and body formats as well. If its not working try debugging line by line.
Sorry I wasn't able to add this as comment

Receiving data from word press

I created custom PHP file in WordPress to send order data to an accounting server via its web service.
Now I need to take amount of goods from that accounting server and update the stock of each product.
I do not access that server, and they don't have any document for their web-service.
I tried to find some info about using web service to do this, but the only thing that I got was to send (not receiving) data of a WordPress site via web service.
This is my code that send order details to accounting server:
<?php
require_once('../wp-config.php');
require_once('../wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php');
$today = new DateTime();
$today->setTime( 0, 0, 0 );
$args = array(
'limit' => 9999,
'return' => 'ids',
'date_completed' => $today,
'status' => 'completed'
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
$url = "http://###.###.###.###:8081/Service1.svc/InsertInvoice";
foreach( $orders as $order_id ) {
$order = wc_get_order( $order_id );
$order_date_completed = $order->get_date_completed();
if (false === strtotime($order_date_completed)) {
echo 'Invalid date for order number '.$order_id.'<br>';
}else {
$order_date_completed->setTime( 0, 0, 0 );
$diff = $today->diff( $order_date_completed );
$diffDays = (integer)$diff->format( "%R%a" );
if($diffDays == 0){
$items = $order->get_items();
$InvoiceInfo = array();
foreach( $items as $item ){
$product = $item->get_product();
$item_sku = $product->get_sku();
$saleType = substr($item_sku, 1, 2);
$item_code = substr($item_sku, 3, 8);
$user_id = substr($item_sku, 11, 6);
$data2 = array(
'CustomerCode' => $user_id,
'SaleTypeNumber' => $saleType,
'ItemCode' => $item_code,
'Quantity' => $item->get_quantity(),
'Fee' => $product->get_price(),
'Discount' => 0,
'Addition' => 0,
'Tax' => 0,
'Duty' => 0,
'Rate' => 1,
'StockCode' => 5,
);
array_push($InvoiceInfo,$data2);
}
$postdata = json_encode($InvoiceInfo);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
}
}
}
?>
In order to send data to WooCommerce, you can use WordPress REST API custom endpoint that can receive data from anywhere.
But, as you didn't provide the name of the accounting server or even any information about the accounting app, you have to check if it is possible to send data from your accounting app to a webhook (with a service like Zapier).

update mailchimp email subscription is not working

Below is my code to create or update subscription in Mailchimp.
function mailchimp_ajax_subscription()
{
$data = isset( $_POST['formData'] ) ? $_POST['formData'] : array();
ob_start();
if(count($data) > 0)
{
$api_key = 'XXXXXXXXXXXXXX';
$status = 'unsubscribed'; // subscribed, unsubscribed, cleaned, pending
$args = array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(array(
'email_address' => $data["email"],
'status' => $status,
'tags' => array($data["name"])
))
);
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/XXXXX/members/' . md5(strtolower($email)), $args );
$body = json_decode( $response['body'] );
if ( $response['response']['code'] == 200 && $body->status == $status ) {
echo 'The user has been successfully ' . $status . '.';
} else {
echo '<b>' . $response['response']['code'] . $body->title . ':</b> ' . $body->detail;
}
}
wp_die();
}
Using above code, I can create subscription into Mailchimp but If I enter same email to edit the tags/status it gives me error.
Error:
400Member Exists: abc#xyz.com is already a list member. Use PUT to
insert or update list members.
I already used PUT in the code so what is missing?
You can use TAGS API to update the TAGS. Below is the sample code to update the TAG:
$postdata = array(
'apikey' => $api_key,
'email_address' => $userData["email"],
'tags' => array(
array(
'name' => $userData["name"],
'status' => 'active'
),
)
);
$mch_api = curl_init(); // initialize cURL connection
curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($userData['email']))."/tags");
curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
curl_setopt($mch_api, CURLOPT_POST, true);
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($postdata) ); // send data in json
$result = curl_exec($mch_api);

Custom WooCommerce Gateway

I`m trying to develop a custom payment gateway for WooCommerce plugin, but I have a problem with the checkout page. What I want is to insert a form on the checkout final step page that is submitted automatically after 5 seconds.
My code is:
...
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action('woocommerce_api_wc_' . $this->id, array($this, 'handle_callback'));
}
function handle_callback() {
wp_die('handle_callback');
}
function receipt_page( $order )
{
echo "receipt page";
$this->generate_submit_form_elements( $order );
}
The problem is that "receipt_page" action is not triggered.
Thanks!
oh, its because you forgot to set the has_fields flag to true.
//after setting the id and method_title, set the has_fields to true
$this -> id = 'kiwipay';
$this -> method_title = 'KiwiPay';
$this->has_fields = true; // if you want credit card payment fields to show on the users checkout page
then in the process_payment function put this:
// Payload would look something like this.
$payload = array(
"amount" => $order.get_total(),
"reference" => $order->get_order_number(),
"orderid" => $order->id,
"return_url" => $this->get_return_url($order) //return to thank you page.
);
response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
// Retrieve the body's response if no errors found
$response_body = wp_remote_retrieve_body( $response );
$response_headers = wp_remote_retrieve_headers( $response );
//use this if you need to redirect the user to the payment page of the bank.
$querystring = http_build_query( $payload );
return array(
'result' => 'success',
'redirect' => $environment_url . '?' . $querystring,
);

Resources