Banner

Banner
Ramast
Linux Developer

Saturday, August 31, 2013

Calling Paypal API

I have had this terrible experience with paypal trying doing something simple like doing an API call.

In my opinion Paypal has the worst nightmare for any developer unlike for example stripe which is a peace of cake compared to it.

Anyway after search many websites excluding developers.paypal.com I found this great page
http://coding.smashingmagazine.com/2011/09/05/getting-started-with-the-paypal-api/

In a nutshell the page present a very simple code for calling any paypal api method.

class Paypal {
   /**
    * Last error message(s)
    * @var array
    */
   protected $_errors = array();

   /**
    * API Credentials
    * Use the correct credentials for the environment in use (Live / Sandbox)
    * @var array
    */
   protected $_credentials = array(
      'USER' => 'seller_1297608781_biz_api1.lionite.com',
      'PWD' => '1297608792',
      'SIGNATURE' => 'A3g66.FS3NAf4mkHn3BDQdpo6JD.ACcPc4wMrInvUEqO3Uapovity47p',
   );

   /**
    * API endpoint
    * Live - https://api-3t.paypal.com/nvp
    * Sandbox - https://api-3t.sandbox.paypal.com/nvp
    * @var string
    */
   protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';

   /**
    * API Version
    * @var string
    */
   protected $_version = '74.0';

   /**
    * Make API request
    *
    * @param string $method string API method to request
    * @param array $params Additional request parameters
    * @return array / boolean Response array / boolean false on failure
    */
   public function request($method,$params = array()) {
      $this -> _errors = array();
      if( empty($method) ) { //Check if API method is not empty
         $this -> _errors = array('API method is missing');
         return false;
      }

      //Our request parameters
      $requestParams = array(
         'METHOD' => $method,
         'VERSION' => $this -> _version
      ) + $this -> _credentials;

      //Building our NVP string
      $request = http_build_query($requestParams + $params);

      //cURL settings
      $curlOptions = array (
         CURLOPT_URL => $this -> _endPoint,
         CURLOPT_VERBOSE => 1,
         //I (ramast) commented the following 3 lines for the sake of simplicity
         //CURLOPT_SSL_VERIFYPEER => true, 
         //CURLOPT_SSL_VERIFYHOST => 2,
         //CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file
         CURLOPT_RETURNTRANSFER => 1,
         CURLOPT_POST => 1,
         CURLOPT_POSTFIELDS => $request
      );

      $ch = curl_init();
      curl_setopt_array($ch,$curlOptions);

      //Sending our request - $response will hold the API response
      $response = curl_exec($ch);

      //Checking for cURL errors
      if (curl_errno($ch)) {
         $this -> _errors = curl_error($ch);
         curl_close($ch);
         return false;
         //Handle errors
      } else  {
         curl_close($ch);
         $responseArray = array();
         parse_str($response,$responseArray); // Break the NVP string to an array
         return $responseArray;
      }
   }
}

After that you can go to https://developer.paypal.com/webapps/developer/docs/classic/api/ find the call you want (make sure you click on NVP not SOAP) Then you can make a call like this
$paypal = new PayPal();
$response = $paypal->request("DoCapture", Array(
        "AUTHORIZATIONID" => "61P02217YA6177331",
        "AMT" => "10.92",
        "COMPLETETYPE" => "Complete",
        "NOTE" => "Thank you for buying from us"));
echo json_encode($response);

Friday, August 9, 2013

avrdude: stk500_recv(): programmer is not responding

I was happily playing with me new arduino uno3 board until they error started to appear and I was unable to upload any more programs to the board

I spent long time searching for a solution or at least understanding what is the problem and in this blog post I will post summary of what I have learned so far

avrdude: stk500_recv(): programmer is not responding error basically means the computer is unable to communicate with the board.

If this is your first try to upload something then there is a possibility you have software issues or problem with drivers.
I am not going to cover this case instead I will assume your board was working fine and suddenly stopped

Why?

  • Bad usb cable
  • Something is connected to pins 0,1 (serial pins)
  • Micro controller chip is fried (atmega328)
  • The chip responsible for communication between the board and PC is fried (ATmega16U2)
  • Something else (use your imagination)

How to determine the cause?


Connect pin 0 with pin 1 on your board
Start the serial monitor window
Type anything and hit enter, did what you type appear in the output box?
If Yes, then the ATmega16U2 is working and its either the cable or the atmega328

Push the reset button, do u see the L led flashing ? If yes then the atmega328 is also fine.

Cable is cheap so u can always try another cable to verify if the cable is fine or not.

How to Fix?

You can replace the atmega328 with a new one (make sure it comes with Uno bootloader)
Like this one
You can also replace your usb cable
I am not sure if its possible to replace the ATmega16U2 IC though

Monday, August 5, 2013

Online electronic shops in Egypt (Review)

This review is about shops selling electronic components like resistors, capacitors up to arduino boards



I

hate shopping in Cairo because of the transportation hell and because of the fact that I don't live in Cairo anymore.
So when electronic fever hit me I started to look for online stores that offer shipping.
And here is my feedback about the ones I found



Future electronics

http://store.fut-electronics.com/index.php

Great online store for beginners.
It explain in details each component and its uses and some possible use cases along with the datasheet and even sometimes some tutorials

The online shop is big with really great varieties  also I like the option of payment upon delivery (so I only pay when the items reach my door)

Its also relatively cheap most components are cheap but also shipping to any place in Cairo (20le) or any place in Egypt (around 45le)

They are official reseller of Sparkfun.com

First Personal experience
I made my first order with them when I was in Cairo in short vacation.
To make the story really short they didn't deliver anything
When I called to ask for the reason of the delay he told me some of the components I ordered is currently not available (damn me!).
Why didn't you call me then? no answer.
being one day before going back home, I had to go myself to their shop and get my stuff.

Important thing to say: He charged me more for at least one component than the price indicated on the website so I would say these online prices are not very reliable.

Second Personal experience
Tried to order from them again (why again? because its cheap) but this time I wanted the items to be shipped to where I live (South Sinai)
I called asking about shipping cost to south sinai he told me it will be estimated and someone will call me to confirm it.
No body called (As expected) and no one answer my calls, emails.
I was making a 500le order so I was expecting some attention but got nothing.
I asked the order be canceled (got no response either).


RAM Electronics

https://ram-e-shop.com

The online shop is also big (bigger than Future Electronics) the option of payment upon delivery is also available

The website is not as beginners friendly as the former one.
Here they are expecting that you know what you are doing also the larger selection makes it harder for a beginner like me to decide which to buy.

You may find some datasheets but in general the website is not very beginner friendly

Prices are expensive either same item in Future Electronics can cost here any thing between 100-200% and sometimes more

Shipping is also more expensive (57le for a less than 1kg shipment to South Sinai compared to 45 in Fut-E)

This shop is also an official distributor for Sparkfun.com

First Personal experience
I knew from the first look this shop is more expensive than FutE and so I have only ordered few things from there that I didn't find in the other store.

After making online order someone called to confirm then I got an SMS few hours later confirming shipment of my items by Fedex, I received my items the next day morning.
(so it was less than 24 hour between submitting the order and receiving it)
Their prices were expensive but accurate, I payed what I expected to pay

Although that experience was good but it doesn't count because I ordered only very few things

Second Personal experience
When FutE turned me down the second time I ordered everything from RAM Electronics .. I found every thing I needed and even many things that I didn't find in FutE all for higher price of course.
Just like first time I got confirmation call from them, then another call informing me one of the items I requested is not available giving me the option to ship without this item or wait until tomorrow and ship everything

the company they use for shipping is egyptexpress/fedex which has a terrible (or rather no) customer support they never picked up the phone when I called.

Despite that the package arrived within two days as promised.
I will definitely order from them again

ElGammal Electronics


http://elgammalelectronics.com/

This shop is direct competitor fo E-Ram, it has lot of components and can be cheaper sometimes than E-Ram. Only down side is that their delivery is very expensive compared to e-ram.


Deal Extreme

Deal Extreme (or dx for short) is general electronic shop not specialized like the previous two but still you could find some electronics in dx and not in the other sites.

Dx is a Chinese so you will only find there things made in China.
Its prices are generally more expensive than those two websites mentioned above but it offer FREE shipping

First Personal experienceI ordered a few items from there to test if its gonna work or will face problem with customs and delivery.
Order was placed 24th of July
Shipped 25th of July (just one day after making the order)
Received 29th of August.

Customs charge was 50% (6$ for my 12$ order)