简体   繁体   English

如何将paypal与codeigniter集成?

[英]how to integrate paypal with codeigniter?

I'm trying to integrate PayPal payment with my codeigniter project.我正在尝试将 PayPal 付款与我的 codeigniter 项目集成。 My project is about a dating site, in which two different users interact with each other.我的项目是关于一个约会网站,其中两个不同的用户相互交互。 So I'm selling plans, on the based of which users will be provided access to send messages to to other for the that period of time.因此,我正在销售计划,基于此计划,用户将有权在该时间段内向其他人发送消息。 For that i have developed a very simple page containing radio buttons to choose from different plans.为此,我开发了一个非常简单的页面,其中包含可以从不同计划中进行选择的单选按钮。 These are the following plans这些是以下计划

  • one month ( $2 )一个月( $2
  • Two month ( $4 )两个月 ( $4 )
  • Three month ( $6 )三个月( $6
  • Four month ( $8 )四个月 ( $8 )
  • Five month ( $10 )五个月( $10

and so on等等

  • Six month ( $12 )六个月( $12

So, user can choose anyone plan from the above and can then proceed further to pay the amount on PayPal.因此,用户可以从上面选择任何计划,然后可以继续在 PayPal 上支付金额。 This page will be available to user who have accounts on our site.此页面可供在我们网站上拥有帐户的用户使用。 so we can transfer all the details of current user to PayPal, such as -- name,email,etc..这样我们就可以将当前用户的所有详细信息传输到PayPal,例如--姓名、电子邮件等。

I have tried reading many articles on how to integrate paypal but none of them helped me.我曾尝试阅读许多关于如何集成贝宝的文章,但没有一篇对我有帮助。 As I'm new to codeigniter.因为我是 codeigniter 的新手。

I have also created Paypal and sandbox account on paypal.我还在贝宝上创建了贝宝和沙盒帐户。 I downloaded php-toolkit from sourceforge.net and used it independently, then it runs properly.我从sourceforge.net下载了php-toolkit,独立使用,运行正常。 but when i try to implement in codeigniter, it does not take anywhere.但是当我尝试在 codeigniter 中实现时,它不会占用任何地方。

Please help me !!请帮帮我!!

EDIT :编辑:

<?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>

This is what i'm simply trying to create.这就是我只是想创造的。 Earlier i tried to use "process.php" in the form action, but i was getting URL error in codeigniter.早些时候,我尝试在表单操作中使用“process.php”,但在 codeigniter 中出现 URL 错误。 So i thought why not use this way.所以我想为什么不使用这种方式。 I know there are function responsible which are sending values to paypal.我知道有负责向贝宝发送值的功能。 But through this action method.但是通过这个动作方法。 I'm being redirected to simple paypal page.我被重定向到简单的贝宝页面。

Here are the steps you can follow.以下是您可以遵循的步骤。

Step1 Create IPN Form. Step1创建IPN表格。 make sure to pass IPN URL (notify URL) to paypal.确保将 IPN URL(通知 URL)传递给 paypal。

For Form variables, you can refer https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/对于表单变量,您可以参考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>

Step 2 Create IPN controller.步骤 2创建 IPN 控制器。 For detailed understanding review https://developer.paypal.com/docs/classic/ipn/gs_IPN/详细了解查看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);
}
$orderide = $this->checkout_model->user_order($order_table);
$business='paypal@business.example.com';            
$cancel_url="http://".$_SERVER['SERVER_NAME']."/checkout/cancel?orderide=".$orderide;
$success_url="http://".$_SERVER['SERVER_NAME']."/checkout/success?order_id=".$orderide;
$data['payment_url']="https://www.sandbox.paypal.com/cgi-bin/webscr?business=$business&cmd=_xclick&item_name=$title&item_number=$product_id&amount=$totalamount&currency_code=EUR&return=$success_url&cancel_return=$cancel_url";              

redirect($data['payment_url']);

In return success URL you can able to get the user id and payment amount作为return success URL您可以获得用户 ID 和付款金额

Step 1: Download the CodeIgniter PayPal library from here: https://github.com/nrshoukhin/codeigniter-paypal-library .第 1 步:从这里下载 CodeIgniter PayPal 库: https : //github.com/nrshoukhin/codeigniter-paypal-library

Step 2: Drag and drop the entire "application" folder from the library to your codeigniter project.第 2 步:将整个“应用程序”文件夹从库中拖放到您的 codeigniter 项目中。 Make sure it's not conflict with your existing files.确保它与您现有的文件不冲突。

Step 3: Load the library from your controller construtor:-第 3 步:从控制器构造函数加载库:-

public function __construct(){
    parent::__construct();
    $this->load->library("paypal");
}

Step 4: Open the file from "application/config/paypal.php".第 4 步:从“application/config/paypal.php”打开文件。 And provide your client ID, client secret and currency.并提供您的客户 ID、客户密码和货币。

Step 5: Create a controller method like below:-第 5 步:创建一个控制器方法,如下所示:-

public function subscribe(){

    if ( !empty($_POST["plan_name"]) && !empty($_POST["plan_description"]) ) {

        $this->paypal->set_api_context();

        $this->paypal->set_plan( $_POST["plan_name"], $_POST["plan_description"], "INFINITE" );

        $definition = "Regular Payments";
        $type       = "REGULAR";
        $frequency  = "MONTH";
        $frequncy_interval = '1';
        $cycles = 0;
        $price = "49";

        $this->paypal->set_billing_plan_definition( $definition, $type, $frequency, $frequncy_interval, $cycles, $price );

        $returnurl = base_url()."payment/success";
        $cancelurl = base_url()."payment/cancel";

        $this->paypal->set_merchant_preferences( $returnurl, $cancelurl );

        $line1 = "Street - 1, Sector - 1";
        $city  = "Dhaka";
        $state = "Dhaka";
        $postalcode = "12345";
        $country = "AU";

        $this->paypal->set_shipping_address( $line1, $city, $state, $postalcode, $country );

        $agreement_name = "Payment Agreement Name";
        $agreement_description = "Payment Agreement Description";

        $this->paypal->create_and_activate_billing_plan( $agreement_name, $agreement_description );

    }

}

Here, set the frequency as "MONTH" and set the frequency_interval according to your desired recurring period.在这里,将频率设置为“MONTH”并根据您想要的重复周期设置frequency_interval。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM