wagtail-longclaw/longclaw/checkout/gateways/braintree.py

86 wiersze
3.0 KiB
Python
Czysty Zwykły widok Historia

2017-02-03 10:04:57 +00:00
import braintree
2017-02-06 08:26:02 +00:00
from longclaw import settings
from longclaw.configuration.models import Configuration
from longclaw.checkout.errors import PaymentError
from longclaw.checkout.gateways import BasePayment
2017-02-03 10:04:57 +00:00
2017-02-06 08:04:04 +00:00
class BraintreePayment(BasePayment):
2017-10-01 10:19:37 +00:00
"""
2017-02-03 10:04:57 +00:00
Create a payment using Braintree
2017-10-01 10:19:37 +00:00
"""
2017-02-03 10:04:57 +00:00
def __init__(self):
if settings.BRAINTREE_SANDBOX:
env = braintree.Environment.Sandbox
else:
env = braintree.Environment.Production
braintree.Configuration.configure(env,
2017-02-03 10:04:57 +00:00
merchant_id=settings.BRAINTREE_MERCHANT_ID,
public_key=settings.BRAINTREE_PUBLIC_KEY,
private_key=settings.BRAINTREE_PRIVATE_KEY)
def create_payment(self, request, amount, description=''):
nonce = request.POST.get('payment_method_nonce')
2017-02-03 10:04:57 +00:00
result = braintree.Transaction.sale({
"amount": str(amount),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
if not result.is_success:
raise PaymentError(result)
2017-03-11 21:55:58 +00:00
return result.transaction.id
2017-02-03 10:04:57 +00:00
def get_token(self, request=None):
# Generate client token
return braintree.ClientToken.generate()
def client_js(self):
return (
2017-06-08 20:07:01 +00:00
"https://js.braintreegateway.com/web/dropin/1.2.0/js/dropin.min.js",
"https://js.braintreegateway.com/web/3.15.0/js/client.min.js",
"https://js.braintreegateway.com/web/3.15.0/js/hosted-fields.min.js"
)
2017-02-03 10:04:57 +00:00
def issue_refund(self, identifier, amount):
result = braintree.Transaction.refund(identifier, amount)
return result.is_success
2017-03-12 18:05:00 +00:00
class PaypalVZeroPayment(BasePayment):
2017-10-01 10:19:37 +00:00
"""
2017-02-03 10:04:57 +00:00
Create a payment using the Paypal/Braintree v.zero SDK
2017-10-01 10:19:37 +00:00
"""
2017-02-03 10:04:57 +00:00
def __init__(self):
self.gateway = braintree.BraintreeGateway(access_token=settings.VZERO_ACCESS_TOKEN)
def create_payment(self, request, amount, description=''):
config = Configuration.for_site(request.site)
nonce = request.POST.get('payment_method_nonce')
2017-02-03 10:04:57 +00:00
result = self.gateway.transaction.sale({
"amount": str(amount),
"payment_method_nonce": nonce,
"merchant_account_id": config.currency,
2017-02-03 10:04:57 +00:00
"options": {
"paypal": {
"description": description
}
}
})
if not result.is_success:
raise PaymentError(result.message)
2017-03-11 21:55:58 +00:00
return result.transaction.order_id
2017-02-03 10:04:57 +00:00
def get_token(self, request):
return self.gateway.client_token.generate()
def client_js(self):
return (
"https://www.paypalobjects.com/api/checkout.js",
"https://js.braintreegateway.com/web/3.15.0/js/client.min.js",
"https://js.braintreegateway.com/web/3.15.0/js/paypal-checkout.min.js"
)
def issue_refund(self, identifier, amount):
return False