php - 如何将 Paypal 与 codeigniter 集成?

标签 php codeigniter paypal paypal-ipn payment-gateway

我正在尝试将 PayPal 支付与我的 codeigniter 项目集成。我的项目是关于一个约会网站,其中两个不同的用户相互交互。因此,我正在销售计划,根据该计划,用户将可以在该时间段内向其他人发送消息。为此,我开发了一个非常简单的页面,其中包含可供选择不同计划的单选按钮。 这些是以下计划

  • one month ($2)
  • Two month ($4)
  • Three month ($6)
  • Four month ($8)
  • Five month ($10)

等等

  • Six month ($12)

因此,用户可以从上面选择任何计划,然后可以继续在 PayPal 上支付金额。该页面将提供给在我们网站上拥有帐户的用户。 这样我们就可以将当前用户的所有详细信息传输到 PayPal,例如 -- 姓名、电子邮件等。

我已经尝试阅读许多关于如何集成 paypal 的文章,但没有一篇对我有帮助。 因为我是 codeigniter 的新手。

我还在 paypal 上创建了 Paypal 和沙箱帐户。我从sourceforge.net上下载了php-toolkit,独立使用,运行正常。但是当我尝试在 codeigniter 中实现时,它不会占用任何空间。

请帮帮我!!

编辑:

<?php 

    //Configuration File
    include_once APPPATH.'../php_paypal/includes/config.inc.php'; 

    //Global Configuration File
    include_once APPPATH.'../php_paypal/includes/global_config.inc.php';

?> 
<?php echo form_open('https://www.sandbox.paypal.com/cgi-bin/webscr');?>   

 <input type="hidden" name="amount" value="9.95">
 <input type="hidden" name="item_name" value="Test Payment">
</form>

这就是我想要创建的。早些时候我尝试在表单操作中使用“process.php”,但我在 codeigniter 中收到 URL 错误。所以我想为什么不使用这种方式。我知道有负责的功能正在向 Paypal 发送值。而是通过这个 Action 方法。我被重定向到简单的 Paypal 页面。

最佳答案

以下是您可以遵循的步骤。

第 1 步 创建 IPN 表格。确保将 IPN URL(通知 URL)传递给 paypal。

Form变量可以引用https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/

<form name="paypalFrm" id="paypalFrm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_ext-enter">
    <input type="hidden" name="redirect_cmd" value="_xclick-subscriptions">
    <input type="hidden" name="return" value="<?php echo $return_url;?>">
    <input type="hidden" name="cancel_return" value="<?php echo $cancel_return;?>">
    <input type="hidden" name="notify_url" value="<?php echo $notify_url;?>">
    <input type="hidden" name="custom" value="<?php echo $custom.",".$custom2;?>">
    <input type="hidden" name="business" value="<?php echo $business_id;?>">
    <input type="hidden" name="item_name" value="<?php echo $item_name;?>">
    <input type="hidden" name="item_number" value="1">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="a3" value="<?php echo $plan_amount;?>">  
    <input type="hidden" name="p3" value="1">  
    <input type="hidden" name="t3" value="M">   
    <input type="hidden" name="src" value="1">
    <input type="hidden" name="sra" value="1">
    <input type="hidden" name="srt" value="12">
    <input type="hidden" name="first_name" value="<?php echo $txtname;?>">
    <input type="hidden" name="lc" value="<?php echo $merchant_country;?>">
    <input type="hidden" name="email" value="<?php echo $txtemail;?>">
</form>

第 2 步 创建 IPN Controller 。详细了解复习https://developer.paypal.com/docs/classic/ipn/gs_IPN/

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }         

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

$header = ''; 
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('www.sandbox.paypal.com', 80, $errno, $errstr, 30);

// assign posted variables to local variables

$content['payment_status']      = $this->input->post('payment_status');
$content['payment_amount']      = $this->input->post('mc_gross');
$content['payment_currency']    = $this->input->post('mc_currency');
$content['txn_id']              = $this->input->post('txn_id');
$content['receiver_email']      = $this->input->post('receiver_email');
$content['payer_email']         = $this->input->post('payer_email');    
$custom                         = explode(",",$this->input->post('custom'));
$content['payment_id']          = $custom[0];
$content['type']                = $custom[1];
$content['txn_type']            = $this->input->post('txn_type');        
$content['paydate']             = date('Y-m-d H:i:s');


if (!$fp)
{
    // HTTP ERROR
}
else
{

    fputs ($fp, $header . $req);
    if (!feof($fp))
    {
        $res = fgets ($fp, 1024);

        if(strcasecmp($content['txn_type'], "subscr_payment") == 0)
        {
            //Action            
        }
        else if(strcasecmp($content['payment_status'], "Completed") == 0)
        {
            //Action            
        }
        else if(strcasecmp($content['txn_type'], "subscr_cancel") == 0)
        {
           //Action            
        }
    }
    fclose ($fp);
}

关于php - 如何将 Paypal 与 codeigniter 集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783050/

相关文章:

paypal - 当我有很多收款人和付款人时,我应该为 Paypal 使用哪个 API

php - 根据 CodeIgniter 上的子字符串查询按 id 进行分组

php - 在 codeigniter 中为每个用户选择最新记录

php - 使用未定义的常量主机 - 假定 'host' Paypal 类中的错误(codeigniter 库)

javascript - 如何使用 PayPal 的 Express In-Context Checkout 和 ReactJS?

PHP preg_replace() 反向引用用作另一个函数的参数

php - 几个逻辑编程问题

Windows 包含 PHP?

php - 为什么 `substr([], 1)` 不返回 "rray"?

paypal - PayPal MEC 库是否已弃用或停产?