Php 如何在PrestaShop中提交付款之前执行操作?

Php 如何在PrestaShop中提交付款之前执行操作?,php,module,prestashop,payment,Php,Module,Prestashop,Payment,我目前正在为PrestaShop 1.6开发一个欺诈检测模块。在这个模块中,我需要在客户按下按钮确认订单之后,但在提交付款之前,调用API,而不考虑付款网关 首先,在查看了1.6中所有可用的钩子之后,我考虑使用actionValidateOrder钩子来实现这一点,问题是这个钩子是在支付网关提交/处理支付后立即执行的,这不是我想要的 我还考虑过使用javascript拦截执行,然后调用验证控制器来执行,但这对于每个网关来说都太具体了,而且prestashop对象/ajax/javascript之

我目前正在为PrestaShop 1.6开发一个欺诈检测模块。在这个模块中,我需要在客户按下按钮确认订单之后,但在提交付款之前,调用API,而不考虑付款网关

首先,在查看了1.6中所有可用的钩子之后,我考虑使用actionValidateOrder钩子来实现这一点,问题是这个钩子是在支付网关提交/处理支付后立即执行的,这不是我想要的

我还考虑过使用javascript拦截执行,然后调用验证控制器来执行,但这对于每个网关来说都太具体了,而且prestashop对象/ajax/javascript之间的数据流似乎可能会出现问题

我知道你们可以创建一个自定义钩子,但我在互联网上看到的每一个例子都是关于显示钩子的,并没有一个演示如何创建自定义动作钩子

这就是我在actionValidateOrder钩子中使用的内容

公共函数hookActionValidateOrder($params)
{
包括一次(_PS_MODULE_DIR_'bayonet/sdk/Paymethods.php');
$this->order=$params['order'];
$cart=$this->context->cart;
$address\u delivery=新地址((int)$cart->id\u address\u delivery);
$address\u invoice=新地址((int)$cart->id\u address\u invoice);
$state\u delivery=新状态((int)$address\u delivery->id\u state);
$country\u delivery=新国家((int)$address\u delivery->id\u country);
$state\u invoice=新状态((int)$address\u invoice->id\u state);
$country\u invoice=新国家((int)$address\u invoice->id\u country);
$customer=$this->context->customer;
$currency=$this->context->currency;
$products=$cart->getProducts();
$product_list=array();
foreach($products as$product)
{
$products_list[]=[
“产品id”=>$product['id\U product'],
“产品名称”=>$product['name'],
“产品价格”=>$product[“价格”],
“产品类别”=>$product['category']
];
}
$request=[
“频道”=>“电子商务”,
“消费者名称”=>$customer->firstname..$customer->lastname,
“消费者内部识别号”=>$customer->id,
“交易金额”=>$cart->getOrderTotal(),
“货币代码”=>$currency->iso\U代码,
“电话”=>$address\u发票->电话,
“电子邮件”=>$customer->email,
“支付网关”=>$this->订单->模块,
“发货地址”=>[
“行1”=>$address\u delivery->address1,
“line_2”=>$address_delivery->address2,
“城市”=>$address\u delivery->city,
“state”=>state\u delivery->name,
“国家”=>转换国家代码($country\u delivery->iso\u code),
“邮政编码”=>$address\u delivery->postcode
],
“账单地址”=>[
“行\u 1”=>$address\u发票->address1,
“行2”=>$address\u发票->address2,
“城市”=>$address\u发票->城市,
“state”=>state\u发票->名称,
“国家”=>转换国家代码($country\u invoice->iso\u code),
“邮政编码”=>$address\u发票->邮政编码
],
“产品”=>$products\U列表,
“订单id”=>(int)$this->order->id
];
foreach($paymentMethods作为$key=>$value){
如果($this->order->module==$key)
{
$request['payment_method']=$value;
如果($this->order->module=='paypalmx')
$request['payment_gateway']='paypal';
}
}
if(配置::get('bayont\u API\u MODE')==0)
{
$this->bayonte=新的bayonteclient([
'api_-key'=>Configuration::get('BAYONET_-api_-TEST_-key'),
'version'=>Configuration::get('BAYONET\u API\u version'))
]);
}
else if(配置::get('bayont\u API\u MODE')==1)
{
$this->bayonte=新的bayonteclient([
'api_-key'=>Configuration::get('BAYONET_-api_-LIVE_-key'),
'version'=>Configuration::get('BAYONET\u API\u version'))
]);
}
$this->刺刀->咨询([
“body”=>$request,
“on_success”=>函数($response){
$this->dataToInsert=array(
'id_cart'=>this->context->cart->id,
“订单号”=>$this->order->id,
“状态”=>$response->decision,
“刺刀跟踪id”=>$response->刺刀跟踪id,
“咨询api”=>1,
“咨询api\U响应”=>json\U编码(数组(
“原因代码”=>$response->reason\u代码,
“跟踪标识”=>$response->刺刀跟踪标识
)),
“是否执行”=>1,
);
Db::getInstance()->insert('bayont',$this->dataToInsert);
如果($response->decision==‘拒绝’)
{
$this->module=module::getInstanceByName('bayont');
工具::重定向($this->context->link->getModuleLink($this->module->name,'rejected',array());
}
},
“on_failure”=>函数($response){
$this->dataToInsert=array(
'id_cart'=>this->context->cart->id,
“订单号”=>$this->order->id,
“状态”=>$response->decision,
“刺刀跟踪id”=>$response->刺刀跟踪id,
“咨询api”=>0,
“咨询api\U响应”=>json\U编码(数组(
“原因代码”=>$response->reason\u代码,
)),
“是否执行”=>1,
);
Db::getInstance()->insert('bayont',$this->dataToInsert);
}
]);
}
在检测到钩子的问题后,这就是我用JavaScript尝试的方法,它使用
$('#form-pagar-mp').submit(function(event) {
    if (lock == 1) {
        params = {};
        $.ajax({
            url: url,
            type: 'post',
            data: params,
            dataType: 'json',
            processData: false,
            success: function(data) {
                if (data.error == 0) {
                    lock = 0;
                }
                else {
                    window.location.href = data.url;
                }
            }
        });
    }   
});
public function hookActionObjectOrderPaymentAddBefore($params)
{
    /** OrderPayment $orderPayment */
    $orderPayment = $params['object'];
    $cart = $this->context->cart;

    // to stop order payment creation you need to redirect from this hook 
}
public function hookActionObjectOrderAddBefore($params)
{
    /** Order $order */
    $order = $params['object'];
    $cart = $this->context->cart;

    // to stop order creation you need to redirect from this hook 
}