Putting credit card payment on your website using Paypal

I created a new stuff on how to put an online credit card payment on your codeigniter website using Paypal Website payment Pro DoDirectPayment and would like to share this to you, hope it helps!.

1. You need to have a paypal account and then apply for a website payment pro, just visit paypal.com (www.paypal.com) for more info.

2. You should have the paypal API Username, Password and Signature, this is important in order for the payment will be credited in your paypal account.

3. On your model, create a new file paypal.php and use my code below:

< ?php

class paypal extends Model {

    function paypal()
    {
        // Call the Model constructor
        parent::Model();
    }

	function PPHttpPost($methodName_, $nvpStr_) {
	// Set up your API credentials, PayPal end point, and API version.
	$environment = 'live'; 										//live or sandbox
	$API_UserName = urlencode('myemail.myemail.com');			//paypal api username
	$API_Password = urlencode('ABCDEFGHIJKLMNOP'); 				//paypal api password
	$API_Signature = urlencode('AbcdEfghijkLmNoPqrStuVwXYz');	//paypal api signature

	if ($environment == 'live')
		$subenvi = '';
	else
		$subenvi = $environment.'.';

	$API_Endpoint = 'https://api-3t.'.$subenvi.'paypal.com/nvp';
	$version = urlencode('51.0'); 								//paypal version

	// Set the curl parameters.
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
	curl_setopt($ch, CURLOPT_VERBOSE, 1);

	// Turn off the server and peer verification (TrustManager Concept).
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);

	// Set the API operation, version, and API signature in the request.
	$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";

	// Set the request as a POST FIELD for curl.
	curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

	// Get response from the server.
	$httpResponse = curl_exec($ch);

	if(!$httpResponse) {
		exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
	}

	// Extract the response details.
	$httpResponseAr = explode("&", $httpResponse);

	$httpParsedResponseAr = array();
	foreach ($httpResponseAr as $i => $value) {
		$tmpAr = explode("=", $value);
		if(sizeof($tmpAr) > 1) {
			$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
		}
	}

	if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
		exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
	}

	return $httpParsedResponseAr;

	} // end function

} //end class

/* End of file paypal.php model */
/* Location: ./system/application/models/paypal.php */
?>

4. On your controller, create a new file checkout.php and use my code below:

< ?php

class checkout extends Controller {

	function checkout() {
		parent::Controller();
	}

	function index() {
		$this->load->model('paypal');

	////////////////////////////////////////////////////////
	//start of paypal module////////////////////////////////
	////////////////////////////////////////////////////////
	// Set request-specific fields.
	$paymentType = urlencode('Sale');					// or 'Sale' or 'Authorization'
	$firstName = urlencode('John');						// first name on credit card
	$lastName = urlencode('Doe');						// last name on credit card
	$creditCardType = urlencode('Visa'); 				// credit card type: Visa, Mastercard, American Express etc.
	$creditCardNumber = urlencode('4444444444444444');	// credit card number
	$expDateMonth = urlencode('06');					// expiry month
	$expDateYear = urlencode('2020');					// expiry year
	$cvv2Number = urlencode('123');						// cvv2 or the last 3/4 digit at the back of credit card
	$address1 = urlencode('address1');					// billing address1
	$address2 = urlencode('address2'); 					// address2
	$city = urlencode('city'); 							// city
	$state = 'CA'; 										// state
	$zip = urlencode('zipcode'); 						// zipcode
	$country = urlencode('country'); 					// US or other valid country code
	$currencyID = urlencode('USD'); 					// USD or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')
	$amount = urlencode('amount'); 						// amount/rate

	// Add request-specific fields to the request string.
	$nvpStr =	"&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber".
				"&EXPDATE=$expDateMonth$expDateYear&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName".
				"&STREET=$address1&CITY=$city&STATE=$state&ZIP=$zip&COUNTRYCODE=$country&CURRENCYCODE=$currencyID";

	// Execute the API operation; see the PPHttpPost function above.
	$httpParsedResponseAr = $this->paypal->PPHttpPost('DoDirectPayment', $nvpStr);  //TURN THIS ON TO MAKE THE PAYMENT LIVE

	if($httpParsedResponseAr["ACK"] == 'Success')
		echo 'Payment was successfully made'
	else
		echo 'Unable to process your payment '.urldecode($httpParsedResponseAr["L_SHORTMESSAGE0"]);

	}// end function

} //end class

/* End of file checkout.php */
/* Location: ./system/application/controllers/checkout.php */

5. If you have any more questions about putting a credit card on your website feel free to contact me archie@ikawka.com

13 comments

  1. Ericeira Glass, Vidro Artistico de Herwig Brunar

    Hello archie, I just saw your blog, as I’m looking for a solution to integrate paypal in my Codeigniter project.

    Do you have any experience with PayPal’s expresscheckout ? (I don’t want to use Paypal Pro yet, as I don’t know how the return will be, if any J)

  2. Yes, i do have experience integrating paypal in codeigniter.

    the fact is that i already developed 3 websites that is now running using codeigniter framework and paypal expresscheckout, it accepts credit card payment.
    http://www.americandreamreview.com
    http://www.adrinclexpn.com
    http://www.adrinclexrn.com

    i also have another ongoing website project that involves social networking, credit card payment thru using codeigniter framework.

  3. Ericeira Glass, Vidro Artistico de Herwig Brunar

    Is there a blog or similar where you gave postet a code example for paypal expresscheckout, like you did for Paypal Pro ?

  4. achilez

    asside from my blog (www.ikawka.com), try the codeigniter forums, but i don’t think you can find similar/relative to my posted codeigniter paypal pro, when i created an interesting code i will just blog it myself in order for me to easily recall when i needed it most

  5. Clemente Edmundo Pichardo Rodríguez

    Firts of all sorry my english , i’m not so quite enough with english , please be patien with me I’m from dominican republic , and i have a question about your integration of Paypal on CI , i saw your sample on this site you posted :
    http://www.ikawka.com/category/paypal/ and i have a lot of troubles trying to integrate a PayPal Samples without CI , and now y need to Integrate , but the method you use for your integration i need to use the checkout express integration , because i need the users of my site enter on the Paypal Account for Pay from there , for legals reasons i don’t want to handle . please can you give me a hand .

  6. achilez

    if you want to use a checkout express integration you just need to point your form into paypal website, by doing that first you should create a paypal button and an html code will be generated and you can use that to put on your website.

  7. Shouvik-S-Mazumdar

    Hey hi, was reading your post in the CI forum . Is your paypal library different from the Paypal Lib posted in the WIKI ? i was able to use the Lib successfully before but for some unknown reasons it has stopped working , so was looking for some better solution.All I want is that the user should be able to pay the order through paypal and i should get a feedback from paypal (IPN) that the order has been completed so that i can update my database. Can you guide me …?

  8. the paypal library i created is different from the paypal lib wiki, it is dedicated only to those who want to use paypal IPN payment using credit card on their website.

  9. Hello,

    Thanks for the wonderful script… I have a small problem. I’m using a sandbox to do the testing before going live. What to I need to change in order to test the sandbox API requests? I’ve made some modifications to your script but I still get the error:

    You do not have permissions to make this API call

    Can you help?

    Thanks.

    D-

  10. aDikSaU

    Hello there,

    Thank you for this wonderful classes you have. These gives me ideas and basic knowledge on how to integrate paypal on CI.

    I’m gonna need some help, an error message keeps on showing everytime I run the code, “Unable to process your payment Security error”.

    Anyone who has idea on how to solve this problem is very much appreciated.

    Thanks in advance…

  11. The company brokerages network will provide you with access to some large pool of individuals who’ve the details about businesses for sale and buyers or investors searching for a organization venture. By making great use from the information you have, you might be cutting a provide and make a handsome profit out from the transactions.

Trackbacks/Pingbacks

  1. Paypal Website Payments Pro: The fastest way to Integrate Paypal? « The Claustrophobic Coder - [...] do on-site payments via Credit Cards only by using the Payment Pro’s  DoDirectPayment API.  A sample code using CodeIgniter ...

Leave a Reply