8bitOrange

  • Archive
  • RSS
  • Ask me anything

Nerd Post: Working with paypal or how I learned to deal with the obnoxious

Paypal…oh paypal, in some ways you make my life so much easier, and yet in others you complicate it to no end. So if you are a developer and you’ve ever had to work with paypal you may have come across their “sandbox”. Now before we go any further I’d like to say that I appreciate paypal’s approach to sanitizing a sandbox for you to test your functionality in, and if I were building a full e-commerce site with paypal as my merchant, this would be a dream place to test and build, but I am not. I am adding a single “Pay Now” button to a form that once submitted comes back to our site and confirms that the person paid their dues. I spent no less then 45 min setting up my sandbox account and getting it working, this is not the first time I’ve had to do this, but it is for where I am now and there is some considerable setup to get it working, and a great bit of reading to understand it. There are differences between account types and I had to verify those difference and also change things to make them work as needed. None of this is to big of a deal over all, but when you add that up to test a single, simple, auto-generated button, well it is a bit of a pain. Really all I wanted was a paypal fake credit card that I could use to test whether the transaction worked or not.

Moving on, because that is not what this is about, lets assume you’ve set up your sandbox account, then setup your fake user and your fake seller account, you’ve made your fake button to test with (that duplicates, hopefully, what the real button you have does), and you are signed in and ready to go. You can do something handy, you can use whats called PDT with paypal and get information back about the transaction you just made. I thought, “wow, great, I can get back an xml feed or json object and handle it”. Nope. You get Text. Plain text, it looks something like this.

SUCCESS
mc_gross=1.00
protection_eligibility=Ineligible
address_status=confirmed
payer_id=xxxxxxxxxx
tax=0.00
address_street=1+Main+St
payment_date=11%3A27%3A08+Apr+09%2C+2010+PDT
payment_status=Pending
charset=windows-1252
address_zip=95131
first_name=Test
mc_fee=0.33
address_country_code=US
address_name=Test+User
custom=
payer_status=unverified
business=xxxxxxx%40mail.com
address_country=United+States
address_city=San+Jose
quantity=1
payer_email=xxxxxxxx%40mail.xom
txn_id=xxxxxxxx
payment_type=instant
btn_id=1145116
last_name=User
address_state=CA
receiver_email=xxxxxx%40mail.com
payment_fee=0.33
shipping_discount=0.00
insurance_amount=0.00
receiver_id=xxxxxxxxx
pending_reason=paymentreview
txn_type=web_accept
item_name=testitem
discount=0.0
mc_currency=USD
item_number=
residence_country=US
shipping_method=Default
handling_amount=0.00
transaction_subject=testitem
payment_gross=1.00
shipping=0.00

handy right? well…its just text, and while strings can be parsed in any language, so can xml, and strings are a pain, how am I supposed to get “payment_gross” regex? I guess, it seems pointless. So I broke up the text and made an array out of it, if you use PHP and want to borrow this, break it, steal it, re-write it, you may do whatever you’d like with it, but it has made my life easier.

$send['tx'] = $_GET['tx'];
$send['at'] = $this->paypal_test;
$send['cmd'] = '_notify-synch';
		
App::import('Core', 'HttpSocket');
$HttpSocket = new HttpSocket();
$url = 'www.paypal.com';
$test_url = 'www.sandbox.paypal.com';
$results = $HttpSocket->post('https://' . $test_url . '/cgi-bin/webscr', $send);\
		
$results = explode("\n", $results);
		
$final_results = array();
if($results[0] == 'SUCCESS'){
	foreach($results as $result){
		if($result != 'SUCCESS'){
			$new_val = explode('=', $result);
			if(!empty($new_val)){
				$final_results[$new_val[0]] = $new_val[1];
			}
		} else {
			$final_results['result'] = $result;
		}
	}
	
} else {
	$final_results['result'] = 'FAIL';
}  

Theres some CakePHP love going on in there (mainly the httpsocket class), but other then that I build the post data array i need to send to paypal, I have my urls setup for prod and test, and then I retrieve my response from paypal. I then break my response into an array by the new line (/n), and begin building my array, luckily I know what format my string is in so there isn’t much need for guessing, but I essentially then break my results array up into arrays from each line and make them a key => value associative array, making it much easier to get at “payment_gross”. Also, if paypal comes back with a fail, set the first result as “fail” and then move on, because paypal does not return much in the way of handy error codes, I felt knowing that it failed would be about all I needed at this point. What I plan to do after this is save a field in the database as “paid” and redirect them to a thank you page.

Hope this helps some paypal devs out there, it may shave a few minutes off your dealing with paypal time.

    • #code
    • #paypal
    • #development
    • #php
    • #cakephp
  • 2 years ago
  • 3
  • Permalink
  • Share
    Tweet

3 Notes/ Hide

  1. engilddespoi liked this
  2. nictitationf liked this
  3. sheenysolemn liked this
  4. latexcrazycheerleaders liked this
  5. majoritypimp liked this
  6. 8bitorange posted this
← Previous • Next →

About

I'm a nerd lost on the internet. I build things with code and write things with words. Fork me on GitHub

Pages

  • I am Matt Caron and I am addicted to the internet
  • Code Blog
  • Resume
  • Stack Overflow
  • My Photography
  • Nerd Appropriate
  • I'm Google-able

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr