我是WooCommerce自定义支付网关(PG)集成的新手。
初始页面:WooCommerce的结帐页面。我根据这里的说明创建了一个自定义插件: http://www.sitepoint.com/building-a-woocommerce-payment-extension/ 和 http://docs.woothemes.com/document/payment-gateway-api/
因此,当我访问结帐页面时,我可以在底部看到我的支付网关。我的代码:
构造:
public function __construct() {
$this->id = 'xxxx_payment_gateway';
$this->title = __( "xxxx Payment Gateway", 'woocommerce-xxxx-gateway' );
$this->icon = null;
$this->method_title = __( 'xxxx Payment Gateway', 'woocommerce-xxxx-gateway' );
$this->method_description = __( 'Take payments via xxxx Payment Gateway - uses the xxxx Payment Gateway SDK.', 'woocommerce-xxxx-gateway' );
$this->has_fields = true;
$this->init_form_fields();
$this->init_settings();
// Save settings
if ( is_admin() ) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
}
}
process_payment
function process_payment( $order_id ) {
$customer_order = wc_get_order( $order_id );
$country_list = array(
"IN"=>"IND",
);
$environment_url = 'http://example.com/pgic/pgserv.php';
$payload = array(
"x_invoice_num" => str_replace( "#", "", $customer_order->get_order_number() ),
"x_merchant_id" => $this->merchant_id,
// Order total
"x_amount" => 3,//$customer_order->order_total,
// Billing Information
"x_first_name" => $customer_order->billing_first_name,
....
// Shipping Information
"x_ship_to_first_name" => $customer_order->shipping_first_name,
....
"x_cust_id" => $customer_order->user_id,
"x_customer_ip" => $_SERVER['REMOTE_ADDR'],
);
$response = wp_remote_post( $environment_url,
array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
)
);
$forwardURL = trim(wp_remote_retrieve_body( $response ));
if ( is_wp_error( $response ) )
{
// Return failure redirect
return array(
'result' => 'failure',
'redirect' => 'failed.php'
);
}
else{
// Reduce stock levels
// $order->reduce_order_stock();
// Remove cart
// WC()->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $forwardURL //$this->get_return_url( $customer_order )
);
}
}//process_payment
我的PG供应商为我提供了以下集成过程。
第1页:上面的插件process_payment传递给支付网关处理到pgserv.php。
第2页:我的银行页面自动出现(由于我使用的银行信用卡),我提供了我的OTP和所有。
第3页:完成该步骤后,支付网关从那里转发到我的网站中的另一个成功登陆页面(让我们称之为 pgresponse.php ),返回交易结果
这就是问题的开始。 我尝试重定向/提交到一个独立页面(submitaction.php),在那里我尝试标记订单完成/付款完成并清空购物车。我的代码:
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$payment_status = $order->payment_complete();
无论我做什么,这种情况下的订单都不会更新状态以完成付款。即使 $ payment_status 也不会返回任何内容。
问题:
我该怎么办?
我打算为process_order_status编写一个钩子来发送邮件。我计划的方式是在插件中编写以下代码:
public function process_order_status( $order, $payment_id, $status, $auth_code ) {
echo "Payment details :: $order, $payment_id, $status, $auth_code";
error_log("Payment details :: $order, $payment_id, $status, $auth_code");
if ( 'APPROVED' == $status ) {
// Payment complete
$order->payment_complete( $payment_id );
// Add order note
$order->add_order_note( sprintf( __( 'Payment approved (ID: %s, Auth Code: %s)', 'woocommerce' ), $payment_id, $auth_code ) );
// Remove cart
WC()->cart->empty_cart();
return true;
}
return false;
}//process_order_status
那会是正确的吗?如果我只能获得更新付款状态的订单,我相信这个方法会被调用,对吗?
我迫切需要一些帮助。任何链接或方向也都可以。非常感谢。
答案 0 :(得分:0)
您需要解码发送给付款服务提供商的有效负载。做这样的事。
public function check_ipn_response($payload)
{
$raw_input = json_decode(file_get_contents('php://input'),TRUE);
$order_id = $raw_input['callback_data'];
// get order_id
$order = new WC_Order($order_id);
// Update order status once order is complete
$order->update_status( 'processing', _x( 'Payment Complete', 'Check payment method', 'wc-gateway-paymentsgy' ) );
wp_die();
}