def GET(self, query): wepay = WePay(IN_PRODUCTION) code = web.input(code='')['code'] try: # try to get a token with our code wepay.get_token(web.ctx.homedomain + '/callback', CLIENT_ID, CLIENT_SECRET, code) # make a new account create_response = wepay.call( '/account/create', { 'name': 'kitty expenses fund', 'description': 'all the money for my kitty' }) # give the account a new picture wepay.call( '/account/modify', { 'account_id': create_response['account_id'], 'image_uri': 'http://www.placekitten.com/500/500' }) # redirect to the new account web.redirect(create_response['account_uri']) except WePay.WePayError as e: return "Received a WePay Error: " + repr(e)
def GET(self, query): wepay = WePay(IN_PRODUCTION) code = web.input(code='')['code'] try: # try to get a token with our code wepay.get_token(web.ctx.homedomain + '/callback', CLIENT_ID, CLIENT_SECRET, code) # make a new account create_response = wepay.call('/account/create', { 'name': 'kitty expenses fund', 'description': 'all the money for my kitty' }) # give the account a new picture wepay.call('/account/modify', { 'account_id': create_response['account_id'], 'image_uri': 'http://www.placekitten.com/500/500' }) # redirect to the new account web.redirect(create_response['account_uri']) except WePay.WePayError as e: return "Received a WePay Error: " + repr(e)
def create_wepay_merchant(code): redirect_uri = settings.WEPAY_REDIRECT_URI client_id = settings.WEPAY_CLIENT_ID client_secret = settings.WEPAY_CLIENT_SECRET production = settings.WEPAY_PRODUCTION # set production to True for live environments wepay = WePay(production, None) # Get a Token to later create an account for a user try: response = wepay.get_token(redirect_uri, client_id, client_secret, code) except: response = "Error" return response
def GET(self, query): wepay = WePay(IN_PRODUCTION) code = web.input(code="")["code"] try: # try to get a token with our code wepay.get_token(web.ctx.homedomain + "/callback", CLIENT_ID, CLIENT_SECRET, code) # make a new account create_response = wepay.call( "/account/create", {"name": "kitty expenses fund", "description": "all the money for my kitty"} ) # give the account a new picture wepay.call( "/account/modify", {"account_id": create_response["account_id"], "image_uri": "http://www.placekitten.com/500/500"}, ) # redirect to the new account web.redirect(create_response["account_uri"]) except WePay.WePayError as e: return "Received a WePay Error: " + repr(e)
def authorize(request): user = request.user farmer = get_object_or_404(Farmer, user=user) production = settings.WEPAY['in_production'] wepay = WePay(production) redirect_uri = settings.WEPAY['authorize_url'] try: if 'code' in request.GET: code = request.GET['code'] token = wepay.get_token( redirect_uri, settings.WEPAY['client_id'], settings.WEPAY['client_secret'], code) if token: farmer.save_access_token(token['access_token']) created = farmer.create_account() if not created[0]: return HttpResponse( "WePay error on update. %s" % error, status=500) # redirect back to profile return HttpResponseRedirect(reverse('profile', args=[user.pk])) else: url = wepay.get_authorization_url( redirect_uri, settings.WEPAY['client_id']) # redirect to authorization url return redirect(url) except WePayError as error: return HttpResponse("WePay error on update. %s" % error, status=500)
class WePayPaymentPlatform(PaymentPlatform): class Meta(PaymentPlatformMeta): key = Service.SERVICE_WEPAY title = "WePay" thumbnail = "/img/media/wepay.png" description = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus placerat venenatis libero vel pellentesque.""" authorization_type = AUTHORIZATION_OAUTH donation_type = DONATION_REDIRECT country = "United States" country_class = "label-info" DEFAULT_AUTH_SCOPE = "manage_accounts,collect_payments,view_user" def __init__(self): PaymentPlatform.__init__(self) self.wepay = WePay( production=settings.FRUCTUS_KEYS.WEPAY_PRODUCTION, store_token=False ) def get_oauth_url(self, redirect_uri, **kwargs): _scope = self.DEFAULT_AUTH_SCOPE if 'scope' in kwargs: _scope = kwargs['scope'] return self.wepay.get_authorization_url( redirect_uri, settings.FRUCTUS_KEYS.WEPAY_CLIENT_ID, scope=_scope ) def service_create(self, owner, **kwargs): if 'code' not in kwargs: raise TypeError() if 'redirect_uri' not in kwargs: raise TypeError() # Get the OAuth token token_result = self.wepay.get_token( kwargs['redirect_uri'], settings.FRUCTUS_KEYS.WEPAY_CLIENT_ID, settings.FRUCTUS_KEYS.WEPAY_CLIENT_SECRET, kwargs['code'] ) if 'error' in token_result: return False, None # Get User Info user_result = self.wepay.call( '/user', token=token_result['access_token'] ) if 'error' in user_result: return False, None if user_result['state'] != 'registered': return False, None # Store details in database return self.db_service_create( owner, Service.SERVICE_WEPAY, token_result['user_id'], link_type=Service.LINK_TYPE_OAUTH, data={ 'access_token': token_result['access_token'], 'token_type': token_result['token_type'], 'email': user_result['email'], 'name': user_result['user_name'], 'first_name': user_result['first_name'], 'last_name': user_result['last_name'] } ) def account_find(self, payee, **kwargs): if not payee or not payee.userservice or not payee.userservice.valid(): return None params = {} if 'name' in kwargs: params['name'] = kwargs['name'] if 'reference_id' in kwargs: params['reference_id'] = kwargs['reference_id'] return self.wepay.call( '/account/find', params, token=payee.userservice.data['access_token'] ) def donation_create(self, entity, recipient, payee, amount, tip=0.0, **kwargs): amount = float(amount) tip = float(tip) if payee is None or payee.userservice is None: return None, None donation = self.db_donation_create(entity, recipient, payee, amount, tip=tip) # Create donation descriptions short_description = self.short_description(recipient, tip) long_description = self.long_description(recipient, amount, tip) params = { 'account_id': payee.account_id, 'short_description': short_description, 'long_description': string_length_limit(long_description, max_length=2047), 'type': 'DONATION', 'amount': amount + tip, 'app_fee': tip, 'fee_payer': 'Payee' } # TODO: this could do with a cleanup if 'base_url' in kwargs: if 'redirect_name' in kwargs: params['redirect_uri'] = kwargs['base_url'] + reverse( kwargs['redirect_name'], args=[donation.token] ) if 'callback_name' in kwargs: params['callback_uri'] = kwargs['base_url'] + reverse( kwargs['callback_name'] ) create_result = self.wepay.call( '/checkout/create', params, token=payee.userservice.data['access_token'] ) if 'checkout_id' in create_result: donation.checkout_id = create_result['checkout_id'] donation.save() if 'checkout_uri' in create_result: return donation, create_result['checkout_uri'] else: return donation, None def donation_confirm(self, donation, **kwargs): pass def donation_update(self, donation): result = self.wepay.call( '/checkout', { 'checkout_id': donation.checkout_id }, token=donation.payee.userservice.data['access_token'] ) if 'error' in result: return False donation.state = result['state'] donation.save() return True