<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eXpand yOur cReativity &#187; paypal</title>
	<atom:link href="http://blog.bhushangahire.net/tag/paypal/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bhushangahire.net</link>
	<description>by Bhushan G Ahire</description>
	<lastBuildDate>Mon, 26 Jul 2010 10:25:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Paypal Instant Payment Notification in Rails with Active Merchant</title>
		<link>http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/</link>
		<comments>http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 08:56:04 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Amazon EC2]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[paypal]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[active merchant]]></category>
		<category><![CDATA[payment]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=139</guid>
		<description><![CDATA[
Active Merchant makes it extremely simple to use Paypal IPN. Here is a simple guide for getting IPN up and running.
Sign up for a Paypal sandbox account
Paypal provides a sandbox environment that mimics their production environment, with the exception that it doesn’t actually process the transactions.  This is extremely useful for development and testing. [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2009%2F08%2F05%2Fpaypal-instant-payment-notification-in-rails-with-active-merchant%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<p><a href="http://home.leetsoft.com/am/">Active Merchant</a> makes it extremely simple to use Paypal <abbr title="Instant Payment Notification"><span class="caps">IPN</span></abbr>. Here is a simple guide for getting <span class="caps">IPN</span> up and running.</p>
<h3>Sign up for a <a href="https://developer.paypal.com/">Paypal sandbox account</a></h3>
<p>Paypal provides a sandbox environment that mimics their production environment, with the exception that it doesn’t actually process the transactions.  This is extremely useful for development and testing.  It allows you to create multiple fake accounts and generate bank accounts and credit cards. More information can be found on Paypal’s <a href="http://www.paypal.com/cgi-bin/webscr?cmd=_ipn-test-about-outside">Testing Instant Payment Notification</a> page.</p>
<p>Unfortunately, I’ve signed up for two different developer accounts and I’ve had trouble logging in with both of them.  I’ve tried resetting my password, but I still can’t log in.  Fortunately, I already have my sandbox accounts set up and don’t really have a need for it (except to write this guide).</p>
<h3>Create a Personal account and add a credit card</h3>
<p>After you sign up for your developer account, create a personal sandbox account and add a credit card.</p>
<h3>Create a Business account and add a checking</h3>
<p>Next, create a business sandbox account and add a checking account.</p>
<h3>Install the money gem</h3>
<pre>sudo gem install money</pre>
<h3>Install the Active Merchant plugin</h3>
<pre>script/plugin install http://activemerchant.googlecode.com/svn/trunk/active_merchant</pre>
<h3>Create a form that submits to Paypal</h3>
<p>Include <code>ActiveMerchant::Billing::Integrations</code> in a controller to add Active Merchant’s helpers.</p>
<pre><code class="ruby"><span class="keywords">class</span> <span class="constants">PaymentsController</span> &lt; <span class="constants">ApplicationController</span>
  include <span class="constants">ActiveMerchant</span>:<span class="symbol">:Billing</span>:<span class="symbol">:Integrations</span>

  <span class="keywords">def</span> create
    <span class="instance">@enrollment</span> = current_user<span class="method">.enrollments</span><span class="method">.find</span><span class="brackets">(</span>params<span class="brackets">[</span><span class="symbol">:id</span><span class="brackets">]</span><span class="brackets">)</span>
  <span class="keywords">end</span>
<span class="keywords">end</span></code></pre>
<p>In the view, use <code>payment_service_for</code> to create a form that submits to Paypal to process the payment.</p>
<pre><code class="erb"><span class="tag">&lt;%</span> payment_service_for <span class="instance">@enrollment</span><span class="method">.id</span>, <span class="constants">PAYPAL_ACCOUNT</span>,
        <span class="symbol">:amount</span> =&gt; <span class="instance">@enrollment</span><span class="method">.course</span><span class="method">.deposit</span>, <span class="symbol">:currency</span> =&gt; <span class="string">'USD'</span>,
        <span class="symbol">:service</span> =&gt; <span class="symbol">:paypal</span> <span class="keywords">do</span> |service|

    service<span class="method">.customer</span> <span class="symbol">:first_name</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.first_name</span>,
        <span class="symbol">:last_name</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.last_name</span>,
        <span class="symbol">:phone</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.phone</span>,
        <span class="symbol">:email</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.email</span>
    service<span class="method">.billing_address</span> <span class="symbol">:city</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.city</span>,
        <span class="symbol">:address1</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.street</span>,
        <span class="symbol">:state</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.state</span>,
        <span class="symbol">:country</span> =&gt; <span class="string">'USA'</span>,
        <span class="symbol">:zip</span> =&gt; <span class="instance">@enrollment</span><span class="method">.student</span><span class="method">.zip</span>
    service<span class="method">.item_name</span> <span class="string">"#{@enrollment.course.program} Deposit"</span>
    service<span class="method">.invoice</span> <span class="instance">@enrollment</span><span class="method">.invoice</span><span class="method">.id</span>
    service<span class="method">.tax</span> <span class="string">'0.00'</span>

    service<span class="method">.notify_url</span> url_for<span class="brackets">(</span><span class="symbol"> <img src='http://blog.bhushangahire.net/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_path</span> =&gt; <span class="constants">false</span>, <span class="symbol">:action</span> =&gt; <span class="string">'notify'</span><span class="brackets">)</span>
    service<span class="method">.return_url</span> url_for<span class="brackets">(</span><span class="symbol"> <img src='http://blog.bhushangahire.net/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_path</span> =&gt; <span class="constants">false</span>,
        <span class="symbol">:controller</span> =&gt; <span class="string">'account'</span>, <span class="symbol">:action</span> =&gt; <span class="string">'show'</span><span class="brackets">)</span>
    service<span class="method">.cancel_return_url</span> url_for<span class="brackets">(</span><span class="symbol"> <img src='http://blog.bhushangahire.net/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_path</span> =&gt; <span class="constants">false</span>,
        <span class="symbol">:controller</span> =&gt; <span class="string">'account'</span>, <span class="symbol">:action</span> =&gt; <span class="string">'show'</span><span class="brackets">)</span> <span class="tag">%&gt;</span>

    &lt;!-- display payment summary here --&gt;

    <span class="tag">&lt;%=</span> submit_tag <span class="string">'Make Payment'</span> <span class="tag">%&gt;</span>
<span class="tag">&lt;%</span> <span class="keywords">end</span> <span class="tag">%&gt;</span></code></pre>
<p>The code above refers to the constant <code>PAYPAL_ACCOUNT</code>, which I define in <code>environment.rb</code>.  I also set Active Merchant to use test mode, which directs it to use Paypal’s sandbox:</p>
<pre><code class="ruby"><span class="keywords">unless</span> <span class="constants">RAILS_ENV</span> == <span class="string">'production'</span>
  <span class="constants">PAYPAL_ACCOUNT</span> = <span class="string">'sandboxaccount@example.com'</span>
  <span class="constants">ActiveMerchant</span>:<span class="symbol">:Billing</span>:<span class="symbol">:Base</span><span class="method">.mode</span> = <span class="symbol">:test</span>
<span class="keywords">else</span>
  <span class="constants">PAYPAL_ACCOUNT</span> = <span class="string">'paypalaccount@example.com'</span>
<span class="keywords">end</span></code></pre>
<h3>Create an action that processes the <span class="caps">IPN</span></h3>
<p>After the above form submits to Paypal and the user makes a payment, Paypal will post data about the transaction to your server.  Set up an action to receive the post:</p>
<pre><code class="ruby">  <span class="keywords">def</span> notify
    notify = <span class="constants">Paypal</span>:<span class="symbol">:Notification</span><span class="method">.new</span><span class="brackets">(</span>request<span class="method">.raw_post</span><span class="brackets">)</span>
    enrollment = <span class="constants">Enrollment</span><span class="method">.find</span><span class="brackets">(</span>notify<span class="method">.item_id</span><span class="brackets">)</span>

    <span class="keywords">if</span> notify<span class="method">.acknowledge</span>
      <span class="instance">@payment</span> = <span class="constants">Payment</span><span class="method">.find_by_confirmation</span><span class="brackets">(</span>notify<span class="method">.transaction_id</span><span class="brackets">)</span> ||
        enrollment<span class="method">.invoice</span><span class="method">.payments</span><span class="method">.create</span><span class="brackets">(</span><span class="symbol">:amount</span> =&gt; notify<span class="method">.amount</span>,
          <span class="symbol">:payment_method</span> =&gt; <span class="string">'paypal'</span>, <span class="symbol">:confirmation</span> =&gt; notify<span class="method">.transaction_id</span>,
          <span class="symbol">:description</span> =&gt; notify<span class="method">.params</span><span class="brackets">[</span><span class="string">'item_name'</span><span class="brackets">]</span>, <span class="symbol">:status</span> =&gt; notify<span class="method">.status</span>,
          <span class="symbol">:test</span> =&gt; notify<span class="method">.test?</span><span class="brackets">)</span>
      begin
        <span class="keywords">if</span> notify<span class="method">.complete?</span>
          <span class="instance">@payment</span><span class="method">.status</span> = notify<span class="method">.status</span>
        <span class="keywords">else</span>
          logger<span class="method">.error</span><span class="brackets">(</span><span class="string">"Failed to verify Paypal's notification, please investigate"</span><span class="brackets">)</span>
        <span class="keywords">end</span>
      <span class="keywords">rescue</span> =&gt; e
        <span class="instance">@payment</span><span class="method">.status</span> = <span class="string">'Error'</span>
        <span class="keywords">raise</span>
      ensure
        <span class="instance">@payment</span><span class="method">.save</span>
      <span class="keywords">end</span>
    <span class="keywords">end</span>
    render <span class="symbol">:nothing</span> =&gt; <span class="constants">true</span>
  <span class="keywords">end</span></code></pre>
<p>Depending on the model for your application, this action will obviously look different. The important part is that you pass the raw post data from the request to <code>Paypal::Notification.new</code>, and call <code>notify.acknowledge</code> to connect back to Paypal to verify the data.</p>
<h3>Enable <span class="caps">IPN</span></h3>
<p>Lastly, log into the business account that you created above, go to <em>“Instant Payment Notification Preferences”</em> in your profile, and set the <span class="caps">URL</span> that Paypal should post back to after payments. (Note: this needs to be a publicly accessible <span class="caps">URL</span>.)</p>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/&amp;title=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/&amp;title=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/&amp;title=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/&amp;t=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant+-+http://tr.im/SU8n+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/&amp;title=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant&amp;summary=Active%20Merchant%20makes%20it%20extremely%20simple%20to%20use%20Paypal%20IPN.%20Here%20is%20a%20simple%20guide%20for%20getting%20IPN%20up%20and%20running.%0A%0ASign%20up%20for%20a%20Paypal%20sandbox%20account%0APaypal%20provides%20a%20sandbox%20environment%20that%20mimics%20their%20production%20environment%2C%20with%20the%20exception%20that%20it%20doesn%E2%80%99t%20actually%20process%20the%20transact&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2009%2F08%2F05%2Fpaypal-instant-payment-notification-in-rails-with-active-merchant%2F&amp;t=Paypal+Instant+Payment+Notification+in+Rails+with+Active+Merchant" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2009/08/05/paypal-instant-payment-notification-in-rails-with-active-merchant/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
