嗯,我需要贝宝的交易细节到我的数据库中。例如项目名称、金额等。
这是我的控制器上的paypal购买功能。我在那里有一个数组,它将会话用户插入到我的数据库中。但我还需要插入物品名称和金额..
public function do_purchase(){
$id = $this->session->userdata('user_id');
$data['un'] = $this->session->userdata('user');
if($data['un'] == NULL){
redirect('error404','location');
}
$config['business'] = 'picturecitytest@gmail.com';
$config['cpp_header_image'] = ''; //Image header url [750 pixels wide by 90 pixels high]
$config['return'] = 'http://localhost/picturecity/myaccount';
$config['cancel_return'] = 'http://localhost/picturecity/cancel_payment';
$config['notify_url'] = 'process_payment.php'; //IPN Post
$config['production'] = FALSE; //Its false by default and will use sandboxe
$config["invoice"] = random_string('numeric',8); //The invoice id
$this->load->library('paypal',$config);
#$this->paypal->add(<name>,<price>,<quantity>[Default 1],<code>[Optional]);
$this->paypal->add('Party Package',2600.00,1); //First item
$transaction = $this->paypal->pay(); //Proccess the payment
$data = array(
"user_id"=>$id,
);
$this->load->database();
$this->db->insert('transaction', $data);
}这是我下载的paypal库
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author Romaldy Minaya
* @copyright Copyright (c) 2011, PROTOS.
* @license GLP
* @since Version 1.0
* @version 1.0
*/
// ------------------------------------------------------------------------
/**
* Paypal Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Payment process
* @author Romaldy Minaya
*
// ------------------------------------------------------------------------
Documentation
This class let you make the payment procces based on paypal API,
effortless and easy.
*1)Use the same documentation about the vars from paypal page. http://bit.ly/j4wRR
*2)Customize the payment procces as you desire.
*3)Build with love.
Implementation
*1)Copy this code in your controller's function
$config['business'] = 'demo@demo.com';
$config['cpp_header_image'] = ''; //Image header url [750 pixels wide by 90 pixels high]
$config['return'] = 'sucess.php';
$config['cancel_return'] = 'shopping.php';
$config['notify_url'] = 'process_payment.php'; //IPN Post
$config['production'] = TRUE; //Its false by default and will use sandbox
$config['discount_rate_cart'] = 20; //This means 20% discount
$config["invoice"] = '843843'; //The invoice id
$this->load->library('paypal',$config);
#$this->paypal->add(<name>,<price>,<quantity>[Default 1],<code>[Optional]);
$this->paypal->add('T-shirt',2.99,6); //First item
$this->paypal->add('Pants',40); //Second item
$this->paypal->add('Blowse',10,10,'B-199-26'); //Third item with code
$this->paypal->pay(); //Proccess the payment
The notify url is where paypal will POST the information of the payment so you
can save that POST directly into your DB and analize as you want.
With $config["invoice"] is how you identify a bill and you can compare,save or update
that value later on your DB.
For test porpuses i do recommend to save the entire POST into your DB and analize if
its working according to your needs before putting it in production mode. EX.
$received_post = print_r($this->input->post(),TRUE);
//Save that variable and analize.
Note: html reference page http://bit.ly/j4wRR
*/
class Paypal {
var $config = Array();
var $production_url = 'https://www.paypal.com/cgi-bin/webscr?';
var $sandbox_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?';
var $item = 1;
/**
* Constructor
*
* @param string
* @return void
*/
public function __construct($props = array())
{
$this->__initialize($props);
log_message('debug', "Paypal Class Initialized");
}
// --------------------------------------------------------------------
/**
* initialize Paypal preferences
*
* @access public
* @param array
* @return bool
*/
function __initialize($props = array())
{
#Account information
$config["business"] = ''; //Account email or id
$config["cmd"] = '_cart'; //Do not modify
$config["production"] = FALSE;
#Custom variable here we send the billing code-->
$config["custom"] = '';
$config["invoice"] = ''; //Code to identify the bill
#API Configuration-->
$config["upload"] = '1'; //Do not modify
$config["currency_code"] = 'PHP'; //http://bit.ly/anciiH
$config["disp_tot"] = 'Y';
#Page Layout -->
$config["cpp_header_image"] = ''; //Image header url [750 pixels wide by 90 pixels high]
$config["cpp_cart_border_color"] = '000'; //The HTML hex code for your principal identifying color
$config["no_note"] = 1; //[0,1] 0 show, 1 hide
#Payment Page Information -->
$config["return"] = ''; //The URL to which PayPal redirects buyers’ browser after they complete their payments.
$config["cancel_return"] = ''; //Specify a URL on your website that displays a “Payment Canceled†page.
$config["notify_url"] = ''; //The URL to which PayPal posts information about the payment (IPN)
$config["rm"] = '2'; //Leave this to get payment information
$config["lc"] = 'EN'; //Languaje [EN,ES]
#Shipping and Misc Information -->
$config["shipping"] = '';
$config["shipping2"] = '';
$config["handling"] = '';
$config["tax"] = '';
$config["discount_amount_cart"] = ''; //Discount amount [9.99]
$config["discount_rate_cart"] = ''; //Discount percentage [15]
#Customer Information -->
$config["first_name"] = '';
$config["last_name"] = '';
$config["address1"] = '';
$config["address2"] = '';
$config["city"] = '';
$config["state"] = '';
$config["zip"] = '';
$config["email"] = '';
$config["night_phone_a"] = '';
$config["night_phone_b"] = '';
$config["night_phone_c"] = '';
/*
* Convert array elements into class variables
*/
if (count($props) > 0)
{
foreach ($props as $key => $val)
{
$config[$key] = $val;
}
}
$this->config = $config;
}
// --------------------------------------------------------------------
/**
* Perform payment process
*
* @access public
* @param array
* @return void
*/
function pay(){
#Convert the array to url encode variables
$vars = http_build_query($this->config);
if($this->config['production'] == TRUE){
header('LOCATION:'.$this->production_url.$vars);
}else{
header('LOCATION:'.$this->sandbox_url.$vars);
}
}
// --------------------------------------------------------------------
/**
* Add a product to the list
*
* @access public
* @param array
* @return void
*/
function add($item_name = '',$item_amount = NULL,$item_qty = NULL,$item_number = NULL){
$this->config['item_name_'.$this->item] = $item_name;
$this->config['amount_'.$this->item] = $item_amount;
$this->config['quantity_'.$this->item] = $item_qty;
$this->config['item_number_'.$this->item] = $item_number;
$this->item++;
}
}
// END Paypal Class
/* End of file Paypal.php */
/* Location: ./application/libraries/Paypal.php */请帮我拿到商品名称和金额。谢谢
发布于 2014-10-10 01:55:37
我使用的是同一个库。这是我的控制器:
if ($this->form_validation->run() == true) {
//send mails to user and admin(for activation)
$this->load->model('mails_m');
$this->mails_m->send_user_email_after_add_website($this->session->userdata('email'));
$this->mails_m->send_admin_email_after_add_website();
//add website
$last_website_id = $this->suggest_m->add_website();
if ( $last_website_id !== '' ) {
$this->session->set_flashdata('message', 'Suggested webiste has been added. Wait to be moderate.');
// start paypal proccess
if ( in_array($this->session->userdata('plan'), array(0,1,2,3,4,5)) ) {
$data_plan = $this->suggest_m->get_plan_data($this->session->userdata('plan'));
$random_key = strtotime("now").rand(1000,9999);
$this->suggest_m->update_website_key($last_website_id, $random_key);
$this->suggest_m->add_payment_key($last_website_id, $random_key);
$config['business'] = 'your_email@domain.com';
$config['return'] = site_url('suggest/pn');
$config['cancel_return'] = site_url('suggest/pc');
$config['notify_url'] = site_url('suggest/pv');
$config['production'] = TRUE;
$config["invoice"] = $random_key;
$this->load->library('paypal/paypal_lib',$config);
$this->paypal_lib->add( $data_plan->name, $data_plan->price , 1, '00'.$data_plan->id);
$this->paypal_lib->pay(); //Proccess the payment
}else{
redirect(base_url());
}
}else{
$this->session->set_flashdata('message', 'Something goes wrong. Please try again.');
$this->load_view('suggest/edit_website/'.$id, $this->data, FALSE);
}
}// DEPRICATED您可以在操作here中看到它。
这是IPN代码。
public function pv(){
$this->load->library('paypal/paypal'); // Load the library
$this->paypal->run();
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
$payment_key = $myPost["invoice"];
if ($myPost !== '') {
$this->suggest_m->update_payment_key($payment_key, $myPost);
$customer_name = $myPost["first_name"].' '.$myPost["last_name"];
//send mails
$this->load->model('mails_m');
$this->mails_m->send_user_email_after_payment($myPost["payer_email"]);
$this->mails_m->send_admin_email_after_payment($myPost["payer_email"], $customer_name);
// calculate item_id
$item_id = ltrim($myPost['item_number1'], '0');
switch ($item_id) {
case '1':
$expire = date("Y-m-d H:i:s");
$expire = strtotime( $date . ' +999 year');
$expire = date("Y-m-d H:i:s", $expire);
$status = 0;
$payed = 1;
$this->suggest_m->update_website_data($status, $payed, $expire, $payment_key);
break;
case '2':
$expire = date("Y-m-d H:i:s");
$expire = strtotime( $date . ' +999 year');
$expire = date("Y-m-d H:i:s", $expire);
$status = 0;
$payed = 1;
$this->suggest_m->update_website_data($status, $payed, $expire, $payment_key);
break;
case '3':
$expire = date("Y-m-d H:i:s");
$expire = strtotime( $date . ' +999 year');
$expire = date("Y-m-d H:i:s", $expire);
$status = 0;
$payed = 1;
$this->suggest_m->update_website_data($status, $payed, $expire, $payment_key);
break;
case '4':
$expire = date("Y-m-d H:i:s");
$expire = strtotime( $date . ' +1 year');
$expire = date("Y-m-d H:i:s", $expire);
$status = 0;
$payed = 1;
$this->suggest_m->update_website_data($status, $payed, $expire, $payment_key);
break;
case '5':
$expire = date("Y-m-d H:i:s");
$expire = strtotime( $date . ' +1 year');
$expire = date("Y-m-d H:i:s", $expire);
$status = 0;
$payed = 1;
$this->suggest_m->update_website_data($status, $payed, $expire, $payment_key);
break;
default:
break;
}
}
}当然,你必须在你的paypal账户中设置ipn url,在我的例子中是www.domain.com/controller_name/pv (),就像上面的函数。
ALso你需要一个额外的库。这是我的IPN简单库。在IPN方法中加载此内容
<?php
class Paypal
{
/*
* 'live' or 'sandbox'
*/
public function __construct($mode = 'sandbox') {
if ($mode == 'live') {
$this->_url = 'https://www.paypal.com/cgi-bin/webscr';
} else {
$this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
}
public function run() {
$postFields = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$postFields .= '&' . $key . '='.urlencode($value);
}
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $this->_url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $postFields,
));
$result = curl_exec($ch);
curl_close($ch);
}
}https://stackoverflow.com/questions/26283983
复制相似问题