JourneyJoker/payment/demo/models.py
Klaus-Uwe Mitterer 329fceea58 Add tabella for pretty data tables
Start demo payment module
Add view for offer selection
2021-04-15 10:34:31 +02:00

64 lines
1.9 KiB
Python

from payment.models import InvoicePayment, Invoice
from .api import PaypalAPI
from paypalcheckoutsdk.orders import OrdersCreateRequest
from paypalhttp import HttpError
from django.db import models
from django.urls import reverse_lazy
from dbsettings.functions import getValue
import logging
logger = logging.getLogger(__name__)
class PaypalOrder(models.Model):
invoice = models.ForeignKey(Invoice, models.CASCADE)
order_id = models.CharField(max_length=64)
class PaypalInvoicePayment(InvoicePayment):
@property
def gateway(self):
return "Paypal"
@staticmethod
def initiate(invoice):
request = OrdersCreateRequest()
request.prefer('return=representation')
request.request_body (
{
"intent": "CAPTURE",
"purchase_units": [
{
"amount": {
"currency_code": invoice.currency,
"value": float(invoice.price_gross)
}
}
],
"application_context": {
"return_url": getValue("application.base_url") + reverse_lazy("this_sucks"),
"cancel_url": getValue("application.base_url"),
"brand_name": getValue("application.name", "JourneyJoker"),
"landing_page": "BILLING",
"user_action": "CONTINUE"
},
}
)
try:
client = PaypalAPI().client
response = client.execute(request)
PaypalOrder.objects.create(subscription=subscription, order_id=response.result.id)
for link in response.result.links:
if link.rel == "approve":
return link.href
except IOError as ioe:
logger.error(ioe)
if isinstance(ioe, HttpError):
logger.error(ioe.status_code)