def _from_token(self, stripe_token, description, charge_type): from urlannotator.payments.stripe_handlers import stripe_client try: stripe = stripe_client() customer = stripe.Customer.create(description=description, card=stripe_token) return self.create( customer_id=customer.id, charge_type=charge_type, ) except stripe.CardError, e: body = e.json_body err = body['error']['message'] raise JobChargeException(err)
def _from_token(self, stripe_token, description, charge_type): from urlannotator.payments.stripe_handlers import stripe_client try: stripe = stripe_client() customer = stripe.Customer.create( description=description, card=stripe_token ) return self.create( customer_id=customer.id, charge_type=charge_type, ) except stripe.CardError, e: body = e.json_body err = body['error']['message'] raise JobChargeException(err)
def charge(self, amount, description=None): """ Charges linked customer with `amount` in USD dollars. """ from urlannotator.payments.stripe_handlers import stripe_client amount_cents = int(amount * 100) currency = self.Currency.USD stripe = stripe_client() desc = description if description else self.get_charge_type_display() data = stripe.Charge.create( amount=amount_cents, currency=currency, customer=self.customer_id, description=desc, ) self.amount = amount_cents self.currency = currency self.charge_id = data['id'] self.save()
def stripe_callback(request): if request.method == "GET": return HttpResponseBadRequest() content = request.body try: content = simplejson.loads(content) except: content = {} event_id = str(content['id']) if 'id' in content else "" if not event_id: log.warning('Stripe callback: Received malformed event id in request.') return HttpResponseBadRequest() # Retrieve event data from Stripe so we will get correct event data stripe = stripe_client() try: event = stripe.Event.retrieve(event_id) event = event.to_dict() except stripe.InvalidRequestError, e: log.exception('Stripe callback: Error while retrieving event') return HttpResponseBadRequest(e.message)