summaryrefslogtreecommitdiff
path: root/payments/stripe/__init__.py
blob: 55be5eece3228bbce9c2c2684ffbe8bc87637ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import unicode_literals
from decimal import Decimal

import stripe

from .forms import ModalPaymentForm, PaymentForm
from .. import RedirectNeeded, PaymentError
from ..core import BasicProvider


class StripeProvider(BasicProvider):

    form_class = ModalPaymentForm

    def __init__(self, public_key, secret_key, image='', name='', **kwargs):
        stripe.api_key = secret_key
        self.secret_key = secret_key
        self.public_key = public_key
        self.image = image
        self.name = name
        super(StripeProvider, self).__init__(**kwargs)

    def get_form(self, payment, data=None):
        if payment.status == 'waiting':
            payment.change_status('input')
        form = self.form_class(
            data=data, payment=payment, provider=self)

        if form.is_valid():
            form.save()
            raise RedirectNeeded(payment.get_success_url())
        return form

    def capture(self, payment, amount=None):
        amount = int((amount or payment.total) * 100)
        charge = stripe.Charge.retrieve(payment.transaction_id)
        try:
            charge.capture(amount=amount)
        except stripe.InvalidRequestError as e:
            payment.change_status('refunded')
            raise PaymentError('Payment already refunded')
        payment.attrs.capture = stripe.util.json.dumps(charge)
        return Decimal(amount) / 100

    def release(self, payment):
        charge = stripe.Charge.retrieve(payment.transaction_id)
        charge.refund()
        payment.attrs.release = stripe.util.json.dumps(charge)

    def refund(self, payment, amount=None):
        amount = int((amount or payment.total) * 100)
        charge = stripe.Charge.retrieve(payment.transaction_id)
        charge.refund(amount=amount)
        payment.attrs.refund = stripe.util.json.dumps(charge)
        return Decimal(amount) / 100


class StripeCardProvider(StripeProvider):

    form_class = PaymentForm