【问题标题】:redirecting user to PayPal after submitting the data to db将数据提交到 db 后将用户重定向到 PayPal
【发布时间】:2013-11-16 00:23:41
【问题描述】:

我在提交表单时经常遇到这种情况。我有一个接受用户订单的表单,在提交表单后,它会将用户带到 PayPal 进行付款。问题是我如何将用户重定向到 PayPal 。我已经尝试了几乎所有可能的解决方案,包括 j 查询的 $.post 函数、JavaScript 双动作提交和 php 查询字符串重定向,但没有人帮助我。这是对你们所有人帮助我的谦卑请求。告诉我解决方案。

附加:表单是自定义的,我创建了模板以将数据存储在 WordPress 中。当我将表单操作的路径更改为我的数据库路径时,for 正在提交到我的数据库,当我将其更改为 PayPal 路径时,它会将其提交到 PayPal 但不提交到我的数据库。

【问题讨论】:

    标签: html wordpress forms paypal


    【解决方案1】:

    类似的代码应该适合您,subscriptionFrm 是您的表单的 ID。在我的例子中,index.php 返回一个带有服务器端处理状态的 JSON 字符串(JSON 字符串用 eval 解析)。当然,您需要将所有 PayPal 隐藏字段附加到您当前的表单中。

    $(document).ready(function () {

    $('#subscriptionFrm input[type="submit"]').click(function () {

              $.post('index.php', $('#subscriptionFrm').serialize()).done(function (r) {
                      var result = eval('(' + r + ')');
                      
                      /* Check result from your PHP code */
                      
                      
                      $('#subscriptionFrm').attr('action', 'https://www.paypal.com/cgi-bin/webscr');
                      $('#subscriptionFrm').submit();
              });
              
              return false;           }); });
    

    【讨论】:

      【解决方案2】:

      在处理表单数据后,有一种非常简单的方法可以重定向到 PayPal。所以你只应该遵循本条目中的原则。

      首先,我从我的插件代码中为 WordPress 初始化设置了一个操作:

      add_action( 'init', 'my_init');
      

      现在,是时候实现 'my_init' 函数了:

      if( !function_exists( 'my_init' ) ){ // Use function_exists to avoid conflicts
          function my_init(){
              if( $_POST[ 'unique_variable' ]){ // A form field to identify we are processing the form
              //...process here your form
              // and then print the form to be redirected to PayPal
              ?>
              <form action="https://www.paypal.com/cgi-bin/webscr" name="myform" method="post">
                 <input type="hidden" name="business" value="seller@email.com" />
             <input type="hidden" name="item_name" value="Product Name" />
                 <input type="hidden" name="item_number" value="Product Number" />
             <input type="hidden" name="amount" value="10" />
             <input type="hidden" name="currency_code" value="USD" />
             <input type="hidden" name="lc" value="EN" />
      
             <input type="hidden" name="return" value="URL to the product after check the payment" />
             <input type="hidden" name="cancel_return" value="URL to use if user cancel the payment process" />
             <input type="hidden" name="notify_url" value="URL of IPN in your website to check the payment" />
      
             <input type="hidden" name="cmd" value="_xclick" />
             <input type="hidden" name="page_style" value="Primary" />
             <input type="hidden" name="no_shipping" value="1" />
             <input type="hidden" name="no_note" value="1" />
             <input type="hidden" name="bn" value="PP-BuyNowBF" />
             <input type="hidden" name="ipn_test" value="1" />
              </form>
              <script type="text/javascript">document.myform.submit();</script>
              <?php
              exit; // It is very important stop the WordPress in this point
              }
      
          }
      }
      

      您应该修改 PayPal 表单字段的值,包括您的电子邮件、IPN 的 URL、取消和退货页面、产品名称、编号和要收取的金额。

      注意打印PayPal表单后的“exit”句,需要停止PHP执行,document.myform.submit();加载后提交 PayPal 表单。

      此主题的一个很好的起点是插件计算字段表单 (http://wordpress.org/plugins/calculated-fields-form/) 的高级版

      ;-) 埃里克

      【讨论】:

        猜你喜欢
        • 2012-01-16
        • 2012-08-25
        • 1970-01-01
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 2021-02-20
        相关资源
        最近更新 更多