这是我提出的解决方案。
感谢@ahmed-ginani 指出脚本。由于您无法直接应用脚本,因此还有其他工作需要做。
基本上这就是我所做的,定义一个自定义的 Paypal ipn 端点。此端点将正确重定向到子域 woocommerce ipn 端点。
这是脚本:
在您启用了 woocommerce paypal 网关的每个子域中。
//first addding the woocommerce paypal ipn in the checkout field, this will be used when paypal sending back to the custom ipn endpoint.
add_filter('woocommerce_paypal_args', 'add_web_paypal_url');
function add_web_paypal_url($args){
$url = site_url();
$ipn = add_query_arg( 'wc-api', 'WC_Gateway_Paypal', $url);
/*we need to merge it into custom field*/
$oldcustom = json_decode($args['custom'], true);
$newcustom = array_merge($oldcustom , array('ipnurl' => $ipn) );
$args['custom'] = json_encode($newcustom);
return $args;
}
然后在主域站点(或您要定义自定义 ipn 端点的站点)中,我们将 paypal 返回数据转发到适当的 ipn 端点。
所以,假设自定义 ipn 端点是 https://www.example.com?action=ipn_forwarder
add_action( 'init', 'paypal_ipn_fowarder' );
function paypal_ipn_fowarder(){
if (isset($_GET['action']) && $_GET['action']=='ipn_forwarder'&& isset($_POST['custom']) ) {
if ( ( $custom = json_decode( $_POST['custom'] ) ) && is_object( $custom ) ) {
$ipnurl = $custom->ipnurl;
if(isset($ipnurl)){
$data = array();
foreach ($_POST as $key => $value) $data []= urlencode($key).'='.urlencode($value);
$data = implode('&', $data);
$ch = curl_init(); /* Initialize */
curl_setopt($ch, CURLOPT_URL, $ipnurl);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch); /* Execute HTTP request */
curl_close($ch); /* Close */
header( 'HTTP/1.1 200 OK', true, 200 );
exit();
}else{
return;
}
}
}
}
就是这样。