Need help on REST API (webservice) for woocommerce checkout - php

How can I write webservice for checkout including payment methods using Woocommerce REST API. Is it available in Woocommerce REST API ? I am new to Woocommerce REST API. Any help is appreciated.

Finally, I figured it out after some research. Below is working code of woocommerce checkout webservice that may Help others -
/*** Just Copy & Paste and change your varriables **/
//do your initial stuff
header('Content-type: application/json');
$json_file=file_get_contents('php://input');
$jsonvalue= json_decode($json_file,true);
$user_id = $jsonvalue['user_id'];
$product_id = $jsonvalue['product_id'];
$quantity = $jsonvalue['quantity'];
//start order data
$orderData = array(
"order" => array(
'payment_method' => 'paypal',
'payment_method_title' => 'Paypal',
'set_paid' => true,
"billing_address" => array(
"first_name" => "bfname",
"last_name" => "blname",
"company" => "testcompanybilling",
"address_1" => "sec8",
"address_2" => "e32",
"city" => "noida",
"state" => "Noida",
"postcode" => "99999",
"country" => "IN",
"email" => "test#gmail.com",
"phone" => "888899999999"
),
"shipping_address" => array(
"first_name" => "sfname",
"last_name" => "slname",
"company" => "testcompanyshipping",
"address_1" => "shakkarpur",
"address_2" => "laxminigar",
"city" => "New Delhi",
"state" => "Delhi",
"postcode" => "110092",
"country" => "IN",
"email" => "testsh#gmail.com",
"phone" => "11009999"
),
"customer_id" => $user_id,
"line_items" => array(
array(
"product_id" => $product_id,
"quantity" => $quantity
)
)
)
);
//Create order usind order data
$data = $client->orders->create($orderData);
//echo '<pre>';
//print_r($data);
$result['success']='true';
$result['error']="0";
$result['msg']='Your order has been successfully placed.';
$result['data']=$data;
echo json_encode($result); `
cheers!!!

You can Refer Woocommerce REST API Documentation for Woocommerce REST API Development.
For Payment Gateways Refer Below URL :
http://woocommerce.github.io/woocommerce-rest-api-docs/#payment-gateways
For Checkout Refer Below URL : WooCommerce API: create order and checkout
I hope this helps.

Related

How to pass a data array from one hooked function to another in WooCommerce

I am doing a call to a partner API using the data contained in woocommerce checkout fields as well as the cart. This first call is made using the checkout process hook to validate the data through the API. if the call returns that it is valid the process goes on, otherwise it stops.
I then need to wait for the payment to be complete to do another call to actually create a plan using the exact same data. Is there a way to pass the array i create in the first call in the checkout process hook to the payment complete hook in order to not have to rebuild the array?
The code would look as follows:
add_action('woocommerce_checkout_process', 'apicall_verif');
function apicall_verif() {
$_ids = array(...);
$billing_fields = [
'billing_first_name' => '',
'billing_last_name' => '',
'billing_email' => '',
'billing_phone' => '',
'insurance-birthdate' => '',
'gender-selection' => '',
'billing_address_1' => '',
'billing_address_2' => '',
'billing_postcode' => '',
'billing_city' => ''
];
foreach( $fields as $field_name => $value ) {
if( !empty( $_POST[$field_name] ) ) {
$fields[$field_name] = sanitize_text_field( $_POST[$field_name] );
}
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( in_array( $cart_item['product_id'], $_ids ) ) {
$_product_id = $cart_item['product_id'];
}
}
$_product = wc_get_product( $_product_id );
$billingcountry = WC()->customer->get_billing_country();
$cur_lang = pll_current_language();
$data_verif = array(
"refs" => array(
"country" => $billingcountry,
),
"settings" => array(
"language" => $cur_lang
),
"policyholder" => array(
"firstName" => $fields['billing_first_name'] ,
"lastName" => $fields['billing_last_name'],
"email" => $fields['billing_email'],
"phone" => $fields['billing_phone'],
"birthdate" => $fields['insurance-birthdate'],
"gender" => $fields['gender-selection' ],
"address" => array(
"country" => $billingcountry,
"zip" => $fields['billing_postcode'],
"city" => $fields['billing_city'],
"street" => $fields['billing_address_1'],
"number" => $fields['billing_address_2'],
"box" => "box"
),
"entityType" => "ENTITY_TYPE_PERSON"
),
"risk" => array(
"model" => $_product -> get_title(),
"originalValue" => $_product -> get_price() * 100,
"antiTheftMeasure" => "ANTI_THEFT_MEASURE_NONE",
),
"terms" => array(
"depreciation" => false
),
);
// Set the array to a custom WC_Session variable (to be used afterwards)
WC()->session->set('data_verif', $data_verif);
// API CALL HERE AND CHECK WHETHER VALID OR NOT
}
//After payment is completed
add_action( 'woocommerce_payment_complete', 'apicall_create' );
function apicall_create() {
//Somehow get $data_verif here and do another API call
}
You can simply set your array in a custom WC_Session variable, that you will be able to use afterwards like:
$data_verif = array(
"refs" => array(
"country" => $billingcountry,
),
"settings" => array(
"language" => $cur_lang
),
"policyholder" => array(
"firstName" => $fields['billing_first_name'] ,
"lastName" => $fields['billing_last_name'],
"email" => $fields['billing_email'],
"phone" => $fields['billing_phone'],
"birthdate" => $fields['insurance-birthdate'],
"gender" => $fields['gender-selection' ],
"address" => array(
"country" => $billingcountry,
"zip" => $fields['billing_postcode'],
"city" => $fields['billing_city'],
"street" => $fields['billing_address_1'],
"number" => $fields['billing_address_2'],
"box" => "box"
),
"entityType" => "ENTITY_TYPE_PERSON"
),
"risk" => array(
"model" => $_product -> get_title(),
"originalValue" => $_product -> get_price() * 100,
"antiTheftMeasure" => "ANTI_THEFT_MEASURE_NONE",
),
"terms" => array(
"depreciation" => false
),
);
// Set the array to a custom WC_Session variable (to be used afterwards)
WC()->session->set('data_verif', $data_verif);
// API CALL HERE AND CHECK WHETHER VALID OR NOT
Then you will be able to call that array in woocommerce_payment_complete hooked function using:
$data_verif = WC()->session->get('data_verif');
This should work.
Now is better to remove (delete) this custom session variable once you have finished to use it… You can do it using the __unset() method like:
WC()->session->__unset('data_verif');
So in your 2nd hooked function:
//After payment is completed
add_action( 'woocommerce_payment_complete', 'apicall_create' );
function apicall_create() {
// Get the data array
WC()->session->get('data_verif');
// Do the API CALL HERE
// Remove the session variable
WC()->session->__unset('data_verif');
}

How to send action plan to followupboss using rest api in php

I'm trying to send leads and inquiry to followupboss crm, this is the php code i used for connecting to the followupboss rest API https://github.com/FollowUpBoss/fub-api-examples
everything is working correctly, Now I have to add action plan json field to the code. but it is not working,
this is my code:
$data = array(
"source" => $propertyname . "-Landing page",
"type" => "Property Inquiry",
"pageTitle" => $propertyname,
"pageUrl" => $prpurl,
"pageDuration" => "",
"message" => "Viewed: " . $propertyname,
"person" => array(
"firstName" => $fname ,
"lastName" => $lname ,
"emails" => array(array("value" => $email)),
"phones" => array(array("value" => $phone)),
"assignedTo" => "Hossein Shahi",
"addresses" => array(
// Address 1
array(
"street" => "322 S Broadway"
)
// If there are more than 1 address, add here
,array(
"street" => ""
)
),
"tags" => array(),
),
"property" => array(
"street" => $propertyname,
"city" => "",
"state" => "",
"code" => "",
"mlsNumber" => $mlscnm,
"price" => $prpprice,
"forRent" => false,
"url" => $prpurl,
"type" => $prptype,
"bedrooms" => $prpbeds,
"bathrooms" => $prpbaths,
"area" => $prparea,
"lot" => 0
),
"actionPlans" => array(
"status" => "Active",
"name" => "Buyer - Pre Construction",
"id" => ""
)
);
actionPlans isn't a valid field for new leads created via the /events API. That's why it's not working.
Action plans created in the UI are automatically triggered when a new lead comes in. Use the app to associate an action plan with an assignee. That is how you ensure an action plan will be in effect for a lead and the person handling it.
Alternatively, you can make a second API call to /actionPlansPeople to enable an action plan for a specific assignee. New items assigned to that user will use the action plan.

2checkout unsuccess error,how to redirect in unsuccess case

I am using 2checkout.i have use the redirect url for success message but how can i get the error message and what are the error message that can be occur after completion of all the requirements.
If you are using 2Checkout's hosted standard or inline checkout, and authorization failure will result in the failure message being displayed to the buyer so that they can correct the details and resubmit. A passback will only be sent to your approved URL when the authorization is successful.
If you are using the 2Checkout Payment API, the error JSON response will be returned to your server.
PHP Example:
Twocheckout::privateKey('BE632CB0-BB29-11E3-AFB6-D99C28100996');
Twocheckout::sellerId('901248204');
// Twocheckout::sandbox(true); #Uncomment to use Sandbox
try {
$charge = Twocheckout_Charge::auth(array(
"merchantOrderId" => "123",
"token" => 'Y2U2OTdlZjMtOGQzMi00MDdkLWJjNGQtMGJhN2IyOTdlN2Ni',
"currency" => 'USD',
"total" => '10.00',
"billingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => 'testingtester#2co.com',
"phoneNumber" => '555-555-5555'
),
"shippingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => 'testingtester#2co.com',
"phoneNumber" => '555-555-5555'
)
), 'array');
if ($charge['response']['responseCode'] == 'APPROVED') {
echo "Thanks for your Order!";
}
} catch (Twocheckout_Error $e) {
$e->getMessage();
}
Please reach out to 2Checkout tech support at techsupport#2co.com if you would like further assistance with your sandbox testing.

Using REST web services add meetings and meetings_contacts relationship using set_relationship method

i have tried to set a relationship between the Meetings created and assigned contacts using rest ,Following is my code .. meetings are inserted successfully but i have no idea why assigned contacts are not saved into a meetings_contacts table
$login_parameters = array(
"user_auth"=>array(
"user_name"=>'rocks',
"password"=>md5('rocks'),
"version"=>"1"
),
"application_name"=>"VanareClient",
"name_value_list"=>array(),
);
$data = call("login", $login_parameters, $url);
$set_contact_parameters = array (
'session' => $data->id,
'module_name' => 'Meetings',
'name_value_list' => array( array (
"name" => "name",
"value" => "Subject"
),
array (
"name" => "description",
"value" => "description"
),
array (
"name" => "location",
"value" => "Pune"
),
array (
"name" => "duration_hours",
"value" =>"1"
),
) );
$dataMeeting = call ( "set_entry", $set_contact_parameters, $url );
$parameters = array(
'session' => $data->id,
'module_name' => 'Meetings',
'module_id' => $dataMeeting->id,
'link_field_name' => 'meetings_contacts',
'related_ids ' => array('25627846-a8a2-eeb5-3565-532035113842'),
);
$dataContactMeetings = call ( "set_relationship", $parameters, $url );
this is what i was trying .. please help me how to insert the relationship of meetings and contacts into a meetings_contacts mysql table . I am Using Sugarcrm CE 6.5.16 version .
Try with
'link_field_name' => 'contacts',
since the link field name is 'contacts' ( the relationship name is 'meetings_contacts' ).

Assigning a variable inside an array

I am working on a wordpress site and I need to assign the value of a function to a value inside an array but it keeps giving me errors. In the customFields array I need to insert the value of the get_option('contact_email', 'no one') into some helper text. In the code snippet below I need to replace {CONTACT_EMAIL} with the get_option value but can't figure it out.
...
var $customFields = array(
array(
"name" => "ContactPerson-name",
"title" => "Contact Name",
"description" => "Enter the first and last name of the page contact. If left blank, the site default of {CONTACT_PERSON} will be used.",
"type" => "textinput",
"scope" => array( "page" ),
"capability" => "edit_pages"
),
array(
"name" => "ContactPerson-email",
"title" => "Contact Email",
"description" => "Enter the email address of the page contact. If left blank, the site default of {CONTACT_EMAIL} will be used.",
"type" => "textinput",
"scope" => array( "page" ),
"capability" => "edit_pages"
),
array(
"name" => "ContactPerson-phone",
"title" => "Contact Phone (XXX) XXX-XXXX",
"description" => "Enter the phone number of the page contact. If left blank, the site default of {CONTACT_PHONE} will be used.",
"type" => "textinput",
"scope" => array( "page" ),
"capability" => "edit_pages"
),
array(
"name" => "ContactPerson-address",
"title" => "Contact Room Number & Building",
"description" => "Enter the room number and building of the page contact. Click here for building abbreviations. If left blank, the site default of {CONTACT_ADDRESS} will be used.",
"type" => "textinput",
"scope" => array( "page" ),
"capability" => "edit_pages"
),
...
...
I've tried to close the text and concat the function, I've tried to do a string replace and nothing seems to work. Thanks for any help anyone can provide.
Brute force method will work...
$customFields = array(
array(
"name" => "ContactPerson-name",
"title" => "Contact Name",
"description" => "... of {CONTACT_PERSON} will be used...",
"type" => "textinput",
"scope" => array( "page" ),
"capability" => "edit_pages"
)
);
$contact_person = get_option('contact_person');
foreach($customFields as &$field)
{
$field['description'] = str_replace("{CONTACT_PERSON}", $contact_person, $field['description']);
}
unset($field);

Categories