Implementing Paypal Subscription API in PHP

I’m working on implementing Paypal Subscription API in PHP for the past weeks, I thought this was kinda complicated just like the DirectPayment method API. Guess what, it was just a piece of cake, i’ll show you my code:


<form name="_xclick" id="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
		<input type="hidden" name="cmd" value="_xclick-subscriptions">
		<input type="hidden" name="business" value="yourpaypalaccount@samplemail.com">
		<input type="hidden" name="currency_code" value="USD">
		<input type="hidden" name="no_shipping" value="1">
		<input type="hidden" name="a3" value="20">
		<input type="hidden" name="p3" value="1">
		<input type="hidden" name="t3" value="M">
		<input type="hidden" name="src" value="1">
		<input type="hidden" name="sra" value="1">
		<input type="hidden" name="return" value="http://mysite.com/return.php">
		<input type="hidden" name="cancel_return" value="http://mysite.com/cancel.php">
		<input type="hidden" name="item_name" value="Subscription Fee">
		<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal">
</form>';

Here’s an explanation about that code, it’s pretty straightforward though.
a3 – amount to billed each recurrence
p3 – number of time periods between each recurrence
t3 – time period (D=days, W=weeks, M=months, Y=years)
return – Return URL after the user has paid
Cancel – the URL when the user clicks on Cancel on the Paypal Page

On this example, there will be a Subscription Button being displayed, and the user will have to click on the button so that they will be redirected to paypal’s website.

Subscribe Button

 

 

If you want something like there’s no user interaction, like there would be no user clicking, just use javascript in sending the form. Just like this:

<script>document.forms['_xclick'].submit();</script>

And don’t forget to wrap the form code with an echo. So it should look like this:

echo '<form name="_xclick" id="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post" style="display:none;">
		<input type="hidden" name="cmd" value="_xclick-subscriptions">
		<input type="hidden" name="business" value="yourpaypalaccount@samplemail.com">
		<input type="hidden" name="currency_code" value="USD">
		<input type="hidden" name="no_shipping" value="1">
		<input type="hidden" name="a3" value="20">
		<input type="hidden" name="p3" value="1">
		<input type="hidden" name="t3" value="M">
		<input type="hidden" name="src" value="1">
		<input type="hidden" name="sra" value="1">
		<input type="hidden" name="return" value="http://mysite.com/return.php">
		<input type="hidden" name="cancel_return" value="http://mysite.com/cancel.php">
		<input type="hidden" name="item_name" value="Subscription Fee">
		<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal">
		</form>';
echo "<script>document.forms['_xclick'].submit();</script>";

Just a piece of cake, right? I hope this helps someone.

Have a happy coding :)

Solving Amazon S3 cURL Error when uploading an Object

Lately, I’ve been working on Amazon S3 API, wherein I’m uploading videos/object in the Amazon Server, however, I was really stuck on an error every time i’m uploading files into the Amazon. Here’s the error below:
Fatal error: Uncaught exception 'RequestCore_Exception' with message 'cURL resource: Resource id #16; cURL error: select/poll returned error (55)' in /home/user/website.com/amazon/lib/requestcore/requestcore.class.php: 824 Stack trace: #0 /home/user/website.com/amazon/services/s3.class.php(882): RequestCore-&gt;send_request() #1 /home/user/website.com/amazon/services/s3.class.php(1391): AmazonS3-&gt;authenticate('bucket...', Array) #2 /home/user/website.com/amazon_upload_video.php(41): AmazonS3-&gt;create_object('bucket...', '1te0f0idn4.mp4', Array) #3 {main} thrown in /home/user/website.com/amazon/lib/requestcore/requestcore.class.php on line 824

Now this is the start of a major headache which I thought I could pass this task quickly! After doing a quick research, I never found an answer, how sad :( , I even asked this at Stackoverflow.com but unfortunately, I never got a correct answer. Anyway, nuf said!

Here’s what I did to combat this bug.

Simple 3 Steps.

  • Make sure to download the Latest Version of Amazon SDK for PHP 
  • After uploading the SDK into your site, point your browser to the location where you uploaded the SDK, then point it where the sdk_compatibility_test.php. Mine was http://my-website.com/location-of-the-sdk/_compatibility_test/sdk_compatibility_test.php. You will see if you’re able to run the Amazon SDK in your site. I assume that you are able to run. Try to check the version of your cURL.
  • Since the error that we encountered was about cURL. Make sure that you have the latest version of  it. Mine is 7.21.0 (OpenSSL/0.9.8o). To update the version of your cURL, try to contact your hosting administrator.

There you go!! That’s how easy to solve that major bug that you’ve been stuck for so long.

So to make the debugging short, make sure you have the latest version of cURL because the lower version of cURL was quite buggy.

There was another solution that I read on how to solve the bug. It was like modifying the config.inc.php and changing the
‘certificate_authority’ => false
into
‘certificate_authority’ => true
but honestly that didn’t work for me, so I guess all of us have a different situation.

Okay! I hope this helps everyone! Have a Happy Coding!