Giter Site home page Giter Site logo

python-sdk's Introduction

WePay Python SDK

WePay's API allows you to easily add payments into your application.

For full documentation, see WePay's developer documentation.

For current and previous versions, see PyPI.

Usage

These examples use the simple web.py application framework.

Setup

Import the module:

from wepay import WePay

Instantiate

Create a new WePay instance. With no arguments, it will use the production version of WePay (www.wepay.com). If called with production=False then it will use the staging version (stage.wepay.com) for testing:

wepay = WePay()

If your user has already authorized your application and you still have the access token, you can instantiate the SDK with the optional access_token parameter. Afterwards, wepay.call() will use the given token for the authorization header:

wepay = WePay(access_token=USERS_ACCESS_TOKEN)

To set an API-Version in the header with your call request, use::

wepay = WePay(production=False, access_token=USERS_ACCESS_TOKEN, api_version=API_VERSION)

Get authorized

Create an authorization url and redirect the user to it. The first parameter is where the user will be redirected back to after they finish authorization. The second is your CLIENT_ID which is provided by WePay:

auth_url = wepay.get_authorization_url(web.ctx.homedomain + '/callback', CLIENT_ID)
web.redirect(auth_url)

Handle the callback

In your method for handling the redirect back to your site (in this case, /callback), you will need to load the GET param code and then call get_token with it. CLIENT_SECRET is provided by WePay. The first parameter of get_token should be the exact same string that was used in get_authorization_url:

code = web.input(code='')['code']

# make sure the first arg is exactly the same as the first arg
# of get_authorization_url
wepay.get_token(web.ctx.homedomain + '/callback', CLIENT_ID, CLIENT_SECRET, code)

The get_token method will automatically load the access token into the WePay instance so that all future API calls will use that token for authorization. It also returns the entire response from the /v2/oauth2/token call if you need any additional data like the WePay ID.

Make some calls

You are now ready to do anything on behalf of your user. Let's start by making a new account:

create_response = wepay.call('/account/create', {
    'name': 'kitty expenses fund',
    'description': 'all the money for my kitty'
})

Now let's set a picture!:

wepay.call('/account/modify', {
    'account_id': create_response['account_id'],
    'image_uri': 'http://www.placekitten.com/500/500'
})

Redirect them to their account page to see it:

web.redirect(create_response['account_uri'])

WePay Risk Headers

You can supply WePay with risk-related information on every API call by passing the WePay-Risk-Token and Client-IP values to the call function:

wepay.call('/account/modify', {
    'account_id': create_response['account_id'],
    'image_uri': 'http://www.placekitten.com/500/500'
}, risk_token='123e4567-e89b-12d3-a456-426655440000', client_ip='100.166.99.123')

Detailed information regarding the Risk Headers can be found at the WePay API Documentation.

Try it!

These examples are put together into a working web app in wepay-example.py. If you already have web.py installed, you can run it by simply doing python wepay-example.py. If you open the app in your browser you should be redirected to WePay to authorize the app. After you authorize, you should get redirected around a bit and end up on your new account page with a kitty picture.

License

MIT License

python-sdk's People

Contributors

abhijeet-wadkar avatar abrutus avatar aroundthesea avatar camerona93 avatar ellardli avatar gentlecat avatar kromped avatar leblanc avatar lukmdo avatar naissa12 avatar thef1rstpancake avatar zedshaw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-sdk's Issues

Cannot parse json

in call function if param is None it should stay None instead of json encoding an empty dictionary.
Example is '/user' API call doesn't require any arguments, and therefore gives an Exception.

Should not catch all exceptions

except:

In the event of some other errors, the exception is just swallowed. For example, a network error. You should list the exceptions that you want to catch.

And I think the code expects that an exception will be raised if the status code is 4xx/5xx, but, as far as I know, it's not the case.

PyPI deployment

It'd be nice to have this on PyPI just to simplify installation, esp for automated deployments.

Select a license

This definitely needs a license with it before too many begin to use the module.

Key Error response['access_token'] before message handled

Oauth2: using the getToken method, basically following this tutorial: https://www.wepay.com/developer/overview/build-a-crowdfunding-site

Okay weird title, but hopefully this will help break it down...

I was (stupidly) using an incorrect client ID/secret, and when wepay/api.py, in the get_token function was returning, I was getting a key error on line 126, where it checks to set the self.access_token.

What I'm finding, is that, if there's an error returned by the previous line (self.call), it will not do anything with that error because the code will error out on the response['access_token'] because 'access_token' won't be set when the code errors out.

Here are a couple screenshots because I'm not great at explaining: http://imgur.com/a/oKcnJ/all

Swapping that line to: self.access_token = response.get('access_token', None)
clears the keyerror, but doesn't do much in terms of helping you know wthat there's a problem. BUT the error is at least there in the response.

So.. yeah. Just some thoughts. I can make a PR for that if you want, or you can just try it out and test it ourself.

local variable 'response' referenced before assignment

The exception handling in api.py lines 68..75 contains a potential error, if the post throws an exception then 'response' in the except will be undefined. This results in the following error.

local variable 'response' referenced before assignment

When reconciling we noticed these errors during the 3/14 maintenance window.

This does not really affect the function (it has failed either way) but the error reported is confusing.

Also this should probably always throw an exception regardless of the status_code.

    try:
        response = self.requests.post(
            url, data=params, headers=headers,
            timeout=self.request_timeout)
        return response.json()
    except:
        if 400 <= response.status_code <= 599:
            raise Exception('Unknown error. Please contact [email protected]')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.