Drupal commerce create paypal wps order programmatically - drupal

I m trying to create a paypal order programmatically but I need the redirection key. The paypal WPS module gets this data from the $order->data['payment_redirect_key'] like this:
// Return to the payment redirect page for processing successful payments
'return' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),
However i cannot find where the payment_redirect_key is created (i.e. which function creates it) in order to create it programmatically. Any help is appreciated.
My goal is to bypass the default drupal commerce checkout mechanism

I'm trying to do the same with no luck yet, but I find out where the payment_redirect_key is created. You can found it in the function commerce_payment_redirect_pane_checkout_form in the commerce/modules/commerce_payment/includes/commerce_payment.checkout_pane.inc function commerce_payment_redirect_pane_checkout_form, line 360 of the current version (http://cgit.drupalcode.org/commerce/tree/modules/payment/includes/commerce_payment.checkout_pane.inc#n360)
Basically, is this:
$order->data['payment_redirect_key'] = drupal_hash_base64(time());
commerce_order_save($order);
EDIT
After a couple days working on this, I found the solution in just a few lines of code. I use this function as a page callback of a menu item (something like product/%sku). Here is the code:
<?php
function custom_module_create_order($sku) {
global $user;
$product = commerce_product_load_by_sku($sku);
$order = ($user->uid) ? commerce_order_new($user->uid, 'checkout_checkout') : commerce_cart_order_new();
// Save to get the Order ID.
commerce_order_save($order);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
commerce_line_item_save($line_item);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
// Select here the payment method. Usually something like:
// [module_name]|commerce_payment_[module_name]
$order->data['payment_method'] = 'commerce_sermepa|commerce_payment_commerce_sermepa';
commerce_order_save($order);
// Set status to order checkout to go to the payment platform redirect.
commerce_order_status_update($order, 'checkout_payment');
drupal_goto('checkout/' . $order->order_id);
}

Related

Wordpress function cannot call submit_button() as it ends with "undefinied function"

i am a real very newbie in coding and in Wordpress. Trying my first test plugin to understand basics. I am able to define plugin, register it, so I can see it in plugins, activate it.
My later goal is to be able to create custom form, save user-specific data to new DB table, and then enable reading/editing it.
I tried to follow the instructions from gmazzap placed here: https://wordpress.stackexchange.com/questions/113936/simple-form-that-saves-to-database
I just am having following error from WP in time of trying to display preview of new screen having a shortcode [userform] in it:
*Fatal error: Uncaught Error: Call to undefined function submit_button() in /data/web/virtuals/131178/virtual/www/subdom/test/system/wp-content/themes/twentytwentythree-child/functions.php:14
*
My functions.php of my theme now looks like this:
<?php
add_action('init', function() {
add_shortcode('userform', 'print_user_form');
});
function print_user_form() {
echo '<form method="POST">';
wp_nonce_field('user_info', 'user_info_nonce', true, true);
?>
All your form inputs (name, email, phone) goes here.
<?php
submit_button('Send Data');
echo '</form>';
}
add_action('template_redirect', function() {
if ( ( is_single() || is_page() ) &&
isset($_POST['user_info_nonce']) &&
wp_verify_nonce($_POST['user_info_nonce'], 'user_info')
) {
// you should do the validation before save data in db.
// I will not write the validation function, is out of scope of this answer
$pass_validation = validate_user_data($_POST);
if ( $pass_validation ) {
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
);
global $wpdb;
// if you have followed my suggestion to name your table using wordpress prefix
$table_name = $wpdb->prefix . 'my_custom_table';
// next line will insert the data
$wpdb->insert($table_name, $data, '%s');
// if you want to retrieve the ID value for the just inserted row use
$rowid = $wpdb->insert_id;
// after we insert we have to redirect user
// I sugest you to cretae another page and title it "Thank You"
// if you do so:
$redirect_page = get_page_by_title('Thank You') ? : get_queried_object();
// previous line if page titled 'Thank You' is not found set the current page
// as the redirection page. Next line get the url of redirect page:
$redirect_url = get_permalink( $redirect_page );
// now redirect
wp_safe_redirect( $redirect_url );
// and stop php
exit();
}
}
});
Note: I have not got to DB exercise, point of my question is submit_button.
As indicated in the error, the code on line 1 points to non identified function:
submit_button('Send Data');
I understood from other discussions submit_button should be core function of WP, so I should be able to call it "directly", without the need of a definition.
I tried following:
originally had very similar code within the plugin, moved to functions.php
reinstalled core of WordPress version 6.1.1
tried several different Themes (as it looked this worked for other users, I tried "classic" and "twentytwentythree" )
And still no little step further, still having same issue with error described above. What I am doing wrong?
If someone would confirm this is WP core installation issue, I am ready to reinstall WP from scratch, just trying to save some time, if there might be other cause.
Thank you for any suggestions.

WooCommerce product update – check if a field's value has changed

I'm using the following code to hook a product update in woocommerce:
add_action('woocommerce_update_product', 'on_update_product', 10, 2);
function on_update_product($product_id, $product){
// code here
}
Is there a way to check if certain fields have changed, compared to the previously stored version of the product?
Thanks!
The best way to do this that I know is with hashes.
add_action('woocommerce_update_product', 'on_update_product', 10, 2);
function on_update_product($product_id, $product){
//create a hash from data you want to track
$hash = md5(json_encode([
$product->get_name(),
$product->get_price(),
"etc....."
]));
//get the hash before the product update
$hashBefore = get_post_meta( $product_id, "hashKey", true );
//check if de hash is diffrend
if ($hash !== $hashBefore) {
// Store the new hash
add_post_meta($product_id, "hashKey", $hash);
// exicute your code
// .....
}
// you can duplicate this process if you want to track individual fields
$hash2 = md5(json_encode([
$product->get_sku(),
]));
$hashBefore2 = get_post_meta( $product_id, "hashKey2", true );
if ($hash2 !== $hashBefore2) {
add_post_meta($product_id, "hashKey2", $hash2);
}
}
To get data out of the product object check this resource:
https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
I hope this suits your situation
I would recommend hooking to another action. I use it to identify changes in orders, but it actually can use for any woocomercce related object types (orders, products, coupons, subscriptions etc.)
woocommerce_before_[objectName]_object_save
for your purpose you can use:
add_action('woocommerce_before_product_object_save', 'identify_product_change', 100, 2);
function identify_product_change($product, $data){
$posted_info = $_POST; // Use this to get the new information
$price = $product->get_price(); //Example of getting the "old" product information
}
Having that said, you need to be careful, since this hook may be initiated from different triggers (some background processes etc). You may want to have some caution measurements:
use $_POST['action'] == 'editpost' to make sure the action is an
actual "Update" click from the admin edit page.
use (is_admin()) to limit it only to admin area
you can use (!defined('DOING_CRON')) to make sure it won't run on any cron execution
and you can use (!defined('DOING_AJAX')) to make sure it won't run on ajax calls
this way you can limit it only to the exact action you wish to catch.

wordpress add payment system. Where to handle form

I wanna to add payment to wordpress . Payment system called PayBox. https://paybox.money/docs/ru/pay-in/3.3#tag/Inicializaciya-cherez-brauzer-polzovatelya/paths/~1payment.php/get . I trying to create form and send data with post method to php file and send request to payment with code below.
<?php
$request = [
'pg_merchant_id'=> 111,
'pg_amount' => 25,
'pg_salt' => 'some_random_string',
'pg_order_id'=>'123',
'pg_description' => 'Описание заказа',
'pg_result_url' => 'https://example.com'
];
// $request['pg_testing_mode'] = 1; //add this parameter to request for testing payments
//if you pass any of your parameters, which you want to get back after the payment, then add them. For example:
// $request['client_name'] = 'My Name';
// $request['client_address'] = 'Earth Planet';
//generate a signature and add it to the array
ksort($request); //sort alphabetically
array_unshift($request, 'payment.php');
array_push($request, 'secret_key'); //add your secret key (you can take it in your personal cabinet on paybox system)
$request['pg_sig'] = md5(implode(';', $request));
unset($request[0], $request[1]);
$query = http_build_query($request);
//redirect a customer to payment page
header('Location:http://core.local/payment.php?'.$query);
But problem is . Where can I handle this code? I dont know how to do this in wp. Form created with page builder .
You need to find out with your page builder that creates the form if they are able to handle hooks. See if there's a function you can hook into when the form is processed.
Generally this would go in your functions.php file of your theme (or a child theme if you're using a ready-made theme).

A Webhook contact form for Discord in wordpress

So I'll start out by saying, that I am a bit new to this.
So I have this website I'm currently making. It's a guild website for World of Warcraft, and we want to be able to have new people being able to apply for membership.
Making the contact form is easy enough through plugins, but this is in theory what I wish to make:
A contact form where when filled in, the application form will push a notification to a webhook set in Discord where when new applicants happen, a message in a channel will be made, notifying the leaders about it.
Do I need to create a plugin myself, or is there any plugin that can offer this functionality?
I had the same needs, after sometime i found a way to do it.
Its actually very simple with WPForms.
WPForms has hooks so you can easily track forms submitions with the wpforms_process_complete hook. This hook allows you to track ALL WPForms sumbission. But maybe you'd like to have different forms. If you wish to track only a specific form, you can add the form id to the end of the hook name.
In my case i had many different forms which are being handled in various different ways, so i had to split them. When a form is being created in WPForms, it receives an ID so does the fields of the named form.
In my case after my form was created it had the following id :
The hook function.
As explained on the Discord Webhook page, Webhooks are a low-effort way to post messages to channels in Discord. They do not require a bot user or authentication to use. The endpoint supports both JSON and form data bodies. In my case i went for JSON.
As explained here you just need to use one of the the content file or embeds field. On this example i will just send a message, so i'll be using the content field.
Once the above instructions applied, you should end up with something close to the following function :
if ( ! function_exists( 'discord_form_submission' ) ) :
/**
* This will fire at the very end of a (successful) form entry.
*
* #link https://wpforms.com/developers/wpforms_process_complete/
*
* #param array $fields Sanitized entry field values/properties.
* #param array $entry Original $_POST global.
* #param array $form_data Form data and settings.
* #param int $entry_id Entry ID. Will return 0 if entry storage is disabled or using WPForms Lite.
*/
function discord_form_submission( $fields, $entry, $form_data, $entry_id )
{
// You have to replace this url by your discord webhook.
$endpoint = 'https://discord.com/api/webhooks/{webhook.id}/{webhook.token}';
// This is the content you can put anything you wish.
// In my case i needed the Name, Class, and the Level of the players.
$content = "**Name :** " . $fields[1]['value'] . PHP_EOL;
$content .= "**Class :** " . $fields[2]['value'] . PHP_EOL;
$content .= "**Level :** " . $fields[3]['value'] . PHP_EOL;
// WP has its own tool to send remote POST request, better use it.
wp_remote_post( $endpoint , [
'headers' => [
'Content-Type' => 'application/json; charset=utf-8'
],
'body' => wp_json_encode([ // Same for the JSON encode.
'content' => $content,
]),
'method' => 'POST',
'data_format' => 'body'
]);
}
endif;
This function must be added in the functions.php file of your theme.
Last but not least, with the help of the WP add_action Function you need to hook up with the wpforms_process_complete hook. In my case since i want to only hook up to the form with the id 1862 i have added the id at the end of the hook which gives us the following code :
add_action( 'wpforms_process_complete_1862', 'discord_form_submission', 10, 4 );
This code must be added in the functions.php file of your theme after our newly added function.

How to get Pay Now URL with custom order status in WooCommerce?

I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callback 
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filter 
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();

Resources