Friday 15 January 2010

php - How to put PayPal transaction details into database in Codeigniter -



php - How to put PayPal transaction details into database in Codeigniter -

well need transaction details of paypal database.. illustration item name, amount, etc.

here's paypal purchase function on controller.. have array there that's 1 inserts sessioned user db.. need insert item name , amount also..

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 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 default , utilize 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 payment $data = array( "user_id"=>$id, ); $this->load->database(); $this->db->insert('transaction', $data); }

this paypal library i've downloaded

<?php if ( ! defined('basepath')) exit('no direct script access allowed'); /** * codeigniter * * open source application development framework 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 class allow create payment procces based on paypal api, effortless , easy. *1)use same documentation vars paypal page. http://bit.ly/j4wrr *2)customize payment procces desire. *3)build love. implementation *1)copy code in controller's function $config['business'] = 'demo@demo.com'; $config['cpp_header_image'] = ''; //image header url [750 pixels wide 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 default , utilize 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 code $this->paypal->pay(); //proccess payment notify url paypal post info of payment can save post straight db , analize want. $config["invoice"] how identify bill , can compare,save or update value later on db. test porpuses recommend save entire post db , analize if working according needs before putting in production mode. ex. $received_post = print_r($this->input->post(),true); //save variable , 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 info $config["business"] = ''; //account email or id $config["cmd"] = '_cart'; //do not modify $config["production"] = false; #custom variable here send billing code--> $config["custom"] = ''; $config["invoice"] = ''; //code identify 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 90 pixels high] $config["cpp_cart_border_color"] = '000'; //the html hex code principal identifying color $config["no_note"] = 1; //[0,1] 0 show, 1 hide #payment page info --> $config["return"] = ''; //the url paypal redirects buyers’ browser after finish payments. $config["cancel_return"] = ''; //specify url on website displays “payment canceled†page. $config["notify_url"] = ''; //the url paypal posts info payment (ipn) $config["rm"] = '2'; //leave payment info $config["lc"] = 'en'; //languaje [en,es] #shipping , misc info --> $config["shipping"] = ''; $config["shipping2"] = ''; $config["handling"] = ''; $config["tax"] = ''; $config["discount_amount_cart"] = ''; //discount amount [9.99] $config["discount_rate_cart"] = ''; //discount percentage [15] #customer info --> $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 class variables */ if (count($props) > 0) { foreach ($props $key => $val) { $config[$key] = $val; } } $this->config = $config; } // -------------------------------------------------------------------- /** * perform payment process * * @access public * @param array * @return void */ function pay(){ #convert array 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 together product 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 */

please help me item name , amount.. thanks

i using same library. here controller :

if ($this->form_validation->run() == true) { //send mails user , 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 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 payment }else{ redirect(base_url()); } }else{ $this->session->set_flashdata('message', 'something goes wrong. please seek again.'); $this->load_view('suggest/edit_website/'.$id, $this->data, false); } }

you can see in action here.

here ipn code.

public function pv(){ $this->load->library('paypal/paypal'); // load 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 $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; } } }

of cource must setup ipn url in paypal business relationship in case www.domain.com/controller_name/pv () funntion above.

also need en library ipn. here ipn simple library. load in ipn method

<?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 $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); } }

php codeigniter paypal

No comments:

Post a Comment