Jump to content

Is there a hook to disable automatic service setup/domain registration for a particular payment method "Stripe"


Go to solution Solved by DennisHermannsen,

Recommended Posts

Hello,

I need a solution to disable an automatic service setup and the automatic domain registration for a particular payment method "Stripe". I need it to review all the orders from stripe to prevent fraud. I know whmcs offers to disable the automatic setup on the product/service page by default. However, that is not a solution because I want to disable automatic setup for a particular gateway only.

I have prepared a hook but it's not working. May someone help me to fix that?

 

<?php
use WHMCS\Database\Capsule;

add_hook('AcceptOrder', 1, function($vars) {
    // Get the order ID
    $orderId = $vars['orderid'];
    
    // Get the order details
    $order = Capsule::table('tblorders')
        ->where('id', $orderId)
        ->first();

    // Check if the payment method is Stripe
    if ($order->paymentmethod == 'stripe') {
        // Log activity
        logActivity("Auto setup prevented for order #{$orderId} using Stripe payment method.");

        // Update the order status to prevent auto-setup
        Capsule::table('tblorders')
            ->where('id', $orderId)
            ->update(['status' => 'Pending']);

        // Return false to abort the auto-setup process
        return false;
    }
});

 

Link to comment
Share on other sites

The AcceptOrder hook point is only triggered when you actually accepts the order - not when the module is provisioning.

You need to use the PreModuleCreate hook point and return abortcmd=true.

You will need to check if the related invoice has been paid with Stripe and only abort the creation if it's not executed by an admin.

Link to comment
Share on other sites

Hello,

 

Thank you for your response, I have tried this but doesn't work

 

<?php
use WHMCS\Database\Capsule;

add_hook('PreModuleCreate', 1, function($vars) {
    // Get the service ID from the hook
    $serviceId = $vars['serviceid'];

    // Fetch the order associated with the service
    $order = Capsule::table('tblhosting')
        ->join('tblorders', 'tblhosting.orderid', '=', 'tblorders.id')
        ->where('tblhosting.id', $serviceId)
        ->select('tblorders.paymentmethod', 'tblorders.id as orderid')
        ->first();

    // Check if the payment method is Stripe
    if ($order->paymentmethod == 'stripe') {
        // Log activity to indicate auto-setup was prevented
        logActivity("Module creation prevented for order #{$order->orderid} due to Stripe payment method.");

        // Abort the module creation (prevent auto-setup)
        return ['abortcmd' => true];
    }
});

 

Link to comment
Share on other sites

I assume you called the hook file "hooks.php" and placed in in the root of your module?
If so, try creating a hook file with the following contents:
 

<?php
add_hook('PreModuleCreate', 1, function($vars) {
    die('Test');
});

When clicking "Create" on the service page, it should just show a "Test" message. If that doesn't happen, your hooks has not been registered correctly.

Link to comment
Share on other sites

Hello,

I am sorry, You have misunderstood the concept I need. I want to use the universal hook of whmcs on this location:  WHMCSDirectory/includes/hooks/disable_auto_provision_stripe.php

I don't want to add a hook for each server module i.e. cpanel, virtualizor etc.

I need a solution to have one single hook file which can prevent any auto-provision of any services/domains/servers etc when the client selects the stripe and pays his invoice through stripe.

I hope you will understand now.

Best Regards

Link to comment
Share on other sites

I think we both understood each other then 🙂
If you're building a module (not an included module in WHMCS) that the hook should be a part of, the hook needs to be registered by disabling and enabling the custom module.
If it's just a standalone hook, you don't need to disable/enable anything.

Did the code in my previous message output "Test" on the service page?

Link to comment
Share on other sites

I have tried this code:

<?php
use WHMCS\Database\Capsule;

add_hook('PreModuleCreate', 1, function($vars) {
    // Get the service ID from the hook
    $serviceId = $vars['serviceid'];

    // Log the service ID for debugging purposes
    logActivity("Service ID: {$serviceId}");
    
    die('Test');
    
});

Inactivity logs:

Service ID:

showing blank!

Link to comment
Share on other sites

Alright, So I have achieved the goal with this code:

<?php

use WHMCS\Database\Capsule;

add_hook('PreModuleCreate', 1, function($vars) {
    // Get the service ID from the hook
    $serviceId = $vars['params']['serviceid'];

    // Fetch the order associated with the service
    $order = Capsule::table('tblhosting')
        ->join('tblorders', 'tblhosting.orderid', '=', 'tblorders.id')
        ->where('tblhosting.id', $serviceId)
        ->select('tblorders.paymentmethod', 'tblorders.id as orderid')
        ->first();

    // Log the detected payment method for debugging
    if ($order) {
        logActivity("Detected payment method: {$order->paymentmethod} for order #{$order->orderid}");
    } else {
        logActivity("Order not found for service ID: {$serviceId}");
    }

    // Check if the payment method is Stripe
    if ($order && $order->paymentmethod == 'stripe') {
        // Log activity to indicate auto-setup was prevented
        logActivity("Module creation prevented for order #{$order->orderid} due to Stripe payment method.");

        // Return an error message to prevent the module creation
        return [
            'abortcmd' => true,
            'error' => 'Module creation has been aborted because the payment method is Stripe.'
        ];
    }
});

But now, When I try to accept the order manually after reviewing the order, it does not let me create the service. I think I have to add more functionality for the accept order. Any help on that thing.

Link to comment
Share on other sites

  • Solution

All of your services should be associated with an order (unless you manually added it somehow).

To have the service created when you manually click, just check if $_SESSION['adminid'] is set:

<?php

use WHMCS\Database\Capsule;

add_hook('PreModuleCreate', 1, function($vars) {
    // Get the service ID from the hook
    $serviceId = $vars['params']['serviceid'];

    // Fetch the order associated with the service
    $order = Capsule::table('tblhosting')
        ->join('tblorders', 'tblhosting.orderid', '=', 'tblorders.id')
        ->where('tblhosting.id', $serviceId)
        ->select('tblorders.paymentmethod', 'tblorders.id as orderid')
        ->first();

    // Log the detected payment method for debugging
    if ($order) {
        logActivity("Detected payment method: {$order->paymentmethod} for order #{$order->orderid}");
    } else {
        logActivity("Order not found for service ID: {$serviceId}");
    }

    // Check if the payment method is Stripe
    if ($order && $order->paymentmethod == 'stripe' && !isset($_SESSION['adminid'])) {
        // Log activity to indicate auto-setup was prevented
        logActivity("Module creation prevented for order #{$order->orderid} due to Stripe payment method.");

        // Return an error message to prevent the module creation
        return [
            'abortcmd' => true,
            'error' => 'Module creation has been aborted because the payment method is Stripe.'
        ];
    }
});
Link to comment
Share on other sites

Just a quick help. When It prevents auto provision, I got this error message on my email: Function Aborted by Action Hook Code (this is a generic message).

How can I change this message to my custom message? So that my staff isn't confused with this error. I would like to change to:

"Order is placed with Stripe, Kindly activate Manually." Something like that. I hope you understand me well.

Link to comment
Share on other sites

Changing the actual email would require another hook.
If it's the 'WHMCS Automatic Setup Failed' email, you could do the following:
Stop the message from being sent using the EmailPreSend hook point (return abortsend=true)
In the same hookpoint, use the SendAdminEmail API function to send the exact same email. It would look something like this:

$command = 'SendAdminEmail';
$postData = array(
    'messagename' => 'WHMCS Automatic Setup Failed',
    'mergefields' => array('client_id' => $clientId, 'service_id' => $serviceId, 'service_product' => $serviceProduct, 'service_domain' => $serviceDomain, 'error' => 'Invoice paid using Stripe. Please check order manually'),
);

$results = localAPI($command, $postData);

The "mergefields" are just the variables used in the template. The variables obviously needs to be defined in your script, otherwise they'll just be blank (which is fine if you don't need them!)

You can also use the same API function to send a custom email. Find the documentation here: https://developers.whmcs.com/api-reference/sendadminemail/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated