我做了一些关于这个话题的研究,有一些专家说这是不可能的,所以我想要一个替代的解决方案。
我的情况:
页面A: [checkout.php]客户填写他们的账单细节。
页面B: [process.php]生成发票号码并在数据库中存储客户详细信息。
页面C: [thirdparty.com]第三支付网关(只接受POST数据)。
客户在页面A中填写详细信息并设置购物车,然后post到页面b。在process.php中,将post数据存储在数据库中并生成发票号码。之后,POST客户数据和发票号码到thirdparty.com支付网关。问题是在页面b中进行POST, cURL能够将数据POST到页面C,但问题是页面没有重定向到页面C。客户需要在页面C中填写信用卡详细信息。
第三方支付网关确实给了我们API样本,样本是POST发票号和客户详细信息。
我们不希望系统生成多余的发票号码。
有什么解决办法吗?
我们目前的解决方案是让客户在页面A中填写详细信息,然后在页面B中创建另一个页面,显示那里的所有客户详细信息,用户可以单击确认按钮以POST到页面C。
我们的目标是让客户只需点击一次。
希望我的问题很清楚:)
/**
* Redirect with POST data.
*
* @param string $url URL.
* @param array $post_data POST data. Example: ['foo' => 'var', 'id' => 123]
* @param array $headers Optional. Extra headers to send.
*/
public function redirect_post($url, array $data, array $headers = null) {
$params = [
'http' => [
'method' => 'POST',
'content' => http_build_query($data)
]
];
if (!is_null($headers)) {
$params['http']['header'] = '';
foreach ($headers as $k => $v) {
$params['http']['header'] .= "$k: $v\n";
}
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if ($fp) {
echo @stream_get_contents($fp);
die();
} else {
// Error
throw new Exception("Error loading '$url', $php_errormsg");
}
}
我有另一个解决方案。它要求客户端运行Javascript(我认为这是一个公平的要求)。
只需在页面A上使用AJAX请求,在后台生成您的发票号码和客户详细信息(之前的页面B),然后一旦请求成功返回正确的信息-只需将表单提交到您的支付网关(页面C)。
这将实现用户只单击一个按钮并继续到支付网关的结果。下面是一些伪代码
HTML:
<form id="paymentForm" method="post" action="https://example.com">
<input type="hidden" id="customInvoiceId" .... />
<input type="hidden" .... />
<input type="submit" id="submitButton" />
</form>
JS(使用jQuery方便,但简单的纯Javascript):
$('#submitButton').click(function(e) {
e.preventDefault(); //This will prevent form from submitting
//Do some stuff like build a list of things being purchased and customer details
$.getJSON('setupOrder.php', {listOfProducts: products, customerDetails: details }, function(data) {
if (!data.error) {
$('#paymentForm #customInvoiceID').val(data.id);
$('#paymentForm').submit(); //Send client to the payment processor
}
});