【问题标题】:PHP replacement for Javascript form auto submit and redirectPHP 替换 Javascript 表单自动提交和重定向
【发布时间】:2012-07-15 12:32:14
【问题描述】:

我正在使用第三方提供商进行信用卡付款,他们需要通过表单发布数据,但是我传递的数据来自数据库,而不是来自填写表单的用户。

目前,我通过一个使用 JavaScript 自动提交的页面来解决这个问题,但是,虽然这可行,但它看起来不是很专业。有没有办法让 PHP 发布到页面然后加载到浏览器中?

<body onload="document.form1.submit()">
<form method="post" action="https://payment.provider.com/Servlet" id="form1" name="form1" target="_top">
    <input type="hidden" name="TRANSACTION_ID" value="<?php echo $transid; ?>" />
    <input type="hidden" name="AMOUNT" value="<?php echo $amount; ?>" />
    <input type="hidden" name="TERMINAL_ID" value="T04_000000000005" />
    <input type="submit" value="Click if not redirected" name="submitButton" />
</form>
</body>

我知道 cURL 当然可以发布数据,但它需要响应。提交表单时,数据会发布到页面,但该页面随后会加载到浏览器中。发布数据后,需要将用户带到第三方提供商页面(该页面使用发布的值,所以我不能简单地进行重定向)。

【问题讨论】:

  • 为什么不能直接发帖?只需在表单中包含那些隐藏字段...
  • 按照我理解您想要的方式,我会看到一个页面假装来自支付处理器,但实际上来自您的网站。那肯定会吓到我……
  • 如果您环顾通过第三方进行支付处理的其他网站(即除了真正的大型网站之外的几乎所有网站),您会发现它们都以这种方式工作——因为支付卡公司需要它。如果您在自己的网站上使用卡付款,请确保以符合 PCI 的方式进行;如果您试图绕过它,当信用卡公司发现时,您会发现自己丢失了您的商家帐户。
  • 我不想“绕过”任何事情,我也不想要一个“假装来自支付处理器”的页面(不知道你为什么会这么想!)。我想要做的就是避免使用 javascript 自动提交的中间页面。经过更多研究,似乎不可能。
  • @Spudley - 我使用了很多不同的支付服务提供商,实际上大多数都允许通过 curl 或类似方式发布,然后是重定向。在这种情况下我必须使用的那个希望它以这种方式完成。

标签: php post curl payment-gateway


【解决方案1】:

是的,我已经找到了解决办法。

在我发现这个解决方案的过程中,我也遇到了一个新的发现。如果您使用计时器打开新页面,Chrome 会阻止该页面。如果您使用按钮打开新页面,Chrome 不会阻止它。

这是我想出的解决方案:

/** This is the script that will redraw current screen and submit to bank. */
echo '<script>'."\n" ;
echo 'function serverNotifySelected()'."\n" ;
echo '{'."\n" ;
echo '    window.open(\'\', \'BankPaymentScreen\');'."\n" ;
echo '    document.forms[\'bank_form\'].submit();'."\n" ;
echo '    document.forms[\'server_responder\'].submit();'."\n" ;
echo '}'."\n" ;
echo '</script>'."\n" ;

/** This form will be opened in a new window called BankPaymentScreen. */
echo '<form action="https://www.sandbox.bank.com/cgi-bin/webscr" name="bank_form" method="post" target="BankPaymentScreen">'."\n" ;
echo '<input type="hidden" name="cmd" value="_s-xclick">'."\n" ;
echo '<input type="hidden" name="custom" value="'.$transaction_start.'">'."\n" ;
echo '<input type="hidden" name="hosted_button_id" value="'.$single_product->hosted_button_id.'">'."\n" ;
echo '<table>'."\n" ;
echo '    <tr>'."\n";
echo '        <td><input type="hidden" name="'.$single_product->hide_name_a.'" value="'.$single_product->hide_value_a.'">Local</td>'."\n" ;
echo '    </tr>'."\n" ;
echo '    <tr>'."\n" ;
echo '        <td>'."\n" ;
echo '        <input type="hidden" name="'.$single_product->hide_name_b.'" value="'.$single_product->hide_value_b.'" />'.$single_product->short_desc.' $'.$adj_price.' USD'."\n" ;
echo '        </td>'."\n" ;
echo '    </tr>'."\n" ;
echo '</table>'."\n" ;
echo '<input type="hidden" name="currency_code" value="USD">'."\n" ;
echo '</form>'."\n" ;

/** This form will redraw the current page for approval. */
echo '<form action="ProductApprove.php" name="server_responder" method="post" target="_top">'."\n" ;
echo '<input type="hidden" name="trans" value="'.$transaction_start.'">'."\n" ;
echo '<input type="hidden" name="prod_id" value="'.$this->product_id.'">'."\n" ;
echo '</form>'."\n" ;

/** No form here just an input and a button.  onClick will handle all the forms */
echo '<input type="image" src="https://www.sandbox.bank.com/en_US/i/btn/btn_purchaseimmediateCC_LG.gif" border="0" alt="This Bank - The safer, easier way to pay!" onclick="serverNotifySelected()">'."\n" ;
echo '<img alt="" border="0" src="https://www.sandbox.bank.com/en_US/i/scr/pixel.gif" width="1" height="1">'."\n" ;

以上代码用于页面上的一个按钮。发生的情况是:您按下按钮,当前页面从 [product to purchase] 变为 [pre-approval],然后打开一个新页面,获得焦点,并传递给支付提供商。

这样,当用户使用完支付服务提供商,并且用户只是关闭支付服务提供商的窗口,并且不使用[返回您的网站]按钮,您的网站已经准备好等待- 批准屏幕!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2016-09-22
    • 2013-09-10
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    相关资源
    最近更新 更多