我正在为WooCommerce开发支付网关插件(For Cybersource Payment)。我几乎在最后开发它,但有一件事是拦截我完成,这是在成功支付回我的wordpress页面后从支付网关网站重定向后的部分(购买确认页面 - 使用模板在wp-创建的新Wordpress页面内容/主题/我的主题/订单确认-的template.php)。但我不知道如何处理从支付网站回来的回复。我寻找一些woocommerce钩子但没有任何效果。我发现有一个挂钩woocommerce_thankyou,但这也不适合我。
有人可以在下面这两点帮助我吗
1)如何处理响应并正确下订单,并在从网关网站成功付款后返回时从购物车中删除商品。
2)我应该从网关网站重定向哪个页面?回到相同的结帐页面或一些自定义页面就像我一样。
非常感谢任何代码帮助。感谢。
答案 0 :(得分:0)
我在我的插件中使用了以下代码,我希望这也适用于你:)
首先添加此代码,
function receipt_page($order){
echo $this -> ResponceHandler($order);
}
现在是ResponceHandler($ order)函数的代码,
public function ResponceHandler($order_id){
if(!isset($_POST['ResponseCode'])){
global $woocommerce;
echo '<p>'.__('Thank you for your order, please click the button below to pay with XYZ', 'woocommerce').'</p>';
$order = new WC_Order($order_id);
$order_id = $order_id.'_'.date("ymds");
$post_data = get_post_meta($order_id,'_post_data',true);
update_post_meta($order_id,'_post_data',array());
###Your Form Code HERE###
echo '<form><input value="Proceed To Payment" type="submit" /> </form>';
}
###Haandle the response###
if(isset($_POST['ResponseCode']))
{
if($_POST['ResponseCode']==0){
global $woocommerce;
session_start();
$_SESSION['post']=$_POST;
$order = new WC_Order($order_id);
$order_id = $order_id.'_'.date("ymds");
$post_data = get_post_meta($order_id,'_post_data',true);
update_post_meta($order_id,'_post_data',array());
if($order->status != 'processing'){
$order ->status ='Processing';
$order->payment_complete();
$order -> add_order_note('XYZ Payment Gateway <br>Response message :'.$_POST['ResponseMessage'].'<br>Payment ID :'.$_POST['PaymentID'].'<br>Merchant Reference Number :'.$_POST['MerchantRefNo'].'<br>Transaction ID :'.$_POST['TransactionID'].'');
add_post_meta( $order->id, '_paymentid', sanitize_text_field( $_POST['PaymentID'] ) );
add_post_meta( $order->id, '_trno', sanitize_text_field( $_POST['TransactionID'] ) );
$woocommerce -> cart -> empty_cart();
wp_redirect( $order->get_checkout_order_received_url());
}
}
else {
if($order->status != 'failed'){
$order ->status ='failed';
echo "Payment failed!<br><br><br>Possible Error : ".$_POST['ResponseMessage']."<br>PaymentID: ".$_POST['PaymentID']."<br><br><br>We request you to save these details for further reference. <br>You can always pay for this order by clicking on your name in the top right corner and visiting your orders section."; }
}
}
}