def confirm_payment(self, request): """ Charges tokenized credit card on Stripe. """ form = PaymentForm(request.POST) if form.is_valid(): self.save_payment_cart(form) token_id = form.cleaned_data['stripe_token'] try: charge = self.charge_amount(self.cart.total, self.settings['PRIVATE_KEY'], token_id) except stripe_api.StripeError as e: return TransactionResult( transaction_id=None, success=False, status='failed', errors={forms.forms.NON_FIELD_ERRORS: [e.message]}) self.cart._cart_state = "SUBMITTED" self.cart.save() handler = StripeIPN(self.cart) handler.accept_payment(charge) return TransactionResult( transaction_id=charge.id, success=True, status='success') else: return TransactionResult( transaction_id=None, success=False, status='failed', errors=form._errors)
def confirm_rev_share_payments(self, request, platform_key, charge_data): form = PaymentForm(request.POST) if form.is_valid(): self.save_payment_cart(form) token_id = form.cleaned_data['stripe_token'] customer = stripe_api.Customer.create( api_key=platform_key, source=token_id, description="Customer for Order %s" % self.cart.pk ) charge_keys = [] try: for cd in charge_data: token = stripe.Token.create( customer=customer.id, stripe_account=cd['stripe_account'] # store's account ) kwargs = { 'application_fee': cd['application_fee'], 'stripe_account': cd['stripe_account'], } charge = self.charge_amount(charge_data['amount'], platform_key, token.id, **kwargs) charge_keys.append([charge, cd['stripe_account']]) except stripe_api.StripeError as e: for charge, key in charge_keys: stripe_api.Refund.create(api_key=key, charge=charge.id) return TransactionResult( transaction_id=None, success=False, status='failed', errors={forms.forms.NON_FIELD_ERRORS: [e.message]}) self.cart._cart_state = "SUBMITTED" self.cart.save() handler = StripeIPN(self.cart) for charge in charges: handler.accept_payment(charge) # We have multiple charges, but whatever, I don't think it matters return TransactionResult( transaction_id=charge.id, success=True, status='success')