Paypal Balance Script

Discussion in 'PHP' started by rhoula, Jul 31, 2013.

  1. #1
    I'm looking for a php code to integrate Paypal users balance into my website.

    Please help. Thank you in Advance
     
    rhoula, Jul 31, 2013 IP
  2. Code Developer

    Code Developer Active Member

    Messages:
    48
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    58
    #2
    I don't think its possible.Paypal will not allow this ever,because of users privacy.You can try your luck with http://apapi.sourceforge.net/ though!
     
    Code Developer, Aug 1, 2013 IP
  3. Phaaze

    Phaaze Member

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #3
    PayPal has a pretty good API that let's you do a lot of things. I'm not sure about querying another users balance, but you can query your own balance.

    Check out the API Developers Docs, most specifically the GetBalance().
    Here is their provided example of doing it via NVP: https://cms.paypal.com/cms_content/US/en_US/files/developer/nvp_GetBalance_php.txt

    Other API call references: https://developer.paypal.com/webapps/developer/docs/classic/api/

    Also, visit PayPal on GitHub to find more of their code examples!
    http://paypal.github.io/

    Hope this helps! :)
     
    Last edited: Aug 1, 2013
    Phaaze, Aug 1, 2013 IP
  4. brandama

    brandama Member

    Messages:
    131
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    35
    #4
    Yes, you can.
    <?php
     
    $environment = 'sandbox';    // or 'beta-sandbox' or 'live'
     
    /**
    * Send HTTP POST Request
    *
    * @param    string    The API method name
    * @param    string    The POST Message fields in &name=value pair format
    * @return    array    Parsed HTTP Response body
    */
    function PPHttpPost($methodName_, $nvpStr_) {
        global $environment;
     
        $API_UserName = urlencode('my_api_username');
        $API_Password = urlencode('my_api_password');
        $API_Signature = urlencode('my_api_signature');
        $API_Endpoint = "https://api-3t.paypal.com/nvp";
        if("sandbox" === $environment || "beta-sandbox" === $environment) {
            $API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
        }
        $version = urlencode('51.0');
     
        // setting the curl parameters.
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
     
        // turning 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);
     
        // NVPRequest for submitting to server
        $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
     
        // setting the nvpreq as POST FIELD to curl
        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
     
        // getting response from server
        $httpResponse = curl_exec($ch);
     
        if(!$httpResponse) {
            exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
        }
     
        // Extract the RefundTransaction 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;
    }
     
    $nvpStr="";
     
    $httpParsedResponseAr = PPHttpPost('GetBalance', $nvpStr);
     
    if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
        exit('GetBalance Completed Successfully: '.print_r($httpParsedResponseAr, true));
    } else  {
        exit('GetBalance failed: ' . print_r($httpParsedResponseAr, true));
    }
     
    ?>
    PHP:
    This will return the balance for a paypal account if the API on that account has been activated and the credentials are correct.
     
    brandama, Aug 2, 2013 IP
  5. Phaaze

    Phaaze Member

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #5
    Ugh, you just posted exactly what I posted... lol :-/

     
    Phaaze, Aug 2, 2013 IP
  6. brandama

    brandama Member

    Messages:
    131
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    35
    #6
    I didn't see any solutions posted, so I posted one. Didn't realize you had linked to one.
     
    brandama, Aug 2, 2013 IP
  7. Phaaze

    Phaaze Member

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #7
    Hehe, it's all good. I did it that way so people would actually read and familiarize themselves with the fact that PayPal has a whole website dedicated to their API and a GitHub with tons of examples. Often, when you simply provide a snippet of code, they don't know how to read/use the snippet/class and just end up asking more questions which would have been answered by the docs. :D
     
    Phaaze, Aug 2, 2013 IP
  8. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #8
    This is what I get when I try that script:

     
    rhoula, Aug 5, 2013 IP
  9. brandama

    brandama Member

    Messages:
    131
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    35
    #9
    Dude, you didn't even thank phaaze for telling you how to do it, now you want him to do it for you?
     
    brandama, Aug 5, 2013 IP
  10. Phaaze

    Phaaze Member

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #10
    Pretty self explanatory message there... The security headers are not valid, therefore you're sending something to PayPal that is not expected. Check that you've entered valid login credentials, that the endpoint or location is what it needs to be, and anything else that may trigger a security error. I've not tested the script I linked you to, it may be old or outdated. That's why I also provided you with links to dive into PayPal's API yourself and to find what you need. Furthermore, if you visit the PayPal GitHub, you can find their OO GetBalance script and PHPUnitTest example.
     
    Phaaze, Aug 5, 2013 IP
  11. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #11
    I tried the same script you posted there few weeks ago. I don't remember what I got but it was completely different from what I got now.

    I was just wondering how does DP do it. It seems to be working great for them. I really appreciate your link to Paypal API link, I've been there before but was lost more than what I'm now.

    Nobody said I wanted someone to do for me. I think you are having some issues with figuring out what is going on around here. If you read my first post I clearly said:

    and I guess Phaaze already knew that, otherwise he would have said something...

    I'm always grateful for the things people have done for me, at the same time I'm more grateful for the people that use common sense and mind there own business other than try to start trouble between others.

    Thank you for being there.
     
    rhoula, Aug 5, 2013 IP