55from django .conf import settings
66
77from .models import CoinifyAPICallback
8- from .models import CoinifyAPIInvoice
8+ from .models import CoinifyAPIPaymentIntent
99from .models import CoinifyAPIRequest
10- from vendor .coinify .coinify_api import CoinifyAPI
1110
1211logger = logging .getLogger ("bornhack.%s" % __name__ )
1312
1413
15- def process_coinify_invoice_json ( invoicejson , order , request ):
16- # create or update the invoice object in our database
17- coinifyinvoice , created = CoinifyAPIInvoice .objects .update_or_create (
18- coinify_id = invoicejson ["id" ],
14+ def process_coinify_payment_intent_json ( intentjson , order , request ):
15+ # create or update the intent object in our database
16+ coinifyintent , created = CoinifyAPIPaymentIntent .objects .update_or_create (
17+ coinify_id = intentjson ["id" ],
1918 order = order ,
20- defaults = {"invoicejson " : invoicejson },
19+ defaults = {"paymentintentjson " : intentjson },
2120 )
2221
2322 # if the order is paid in full call the mark as paid method now
24- if invoicejson ["state" ] == "complete" and not coinifyinvoice .order .paid :
25- coinifyinvoice .order .mark_as_paid (request = request )
23+ if "state" in intentjson :
24+ if intentjson ["state" ] == "complete" and not coinifyintent .order .paid :
25+ coinifyintent .order .mark_as_paid (request = request )
2626
27- return coinifyinvoice
27+ return coinifyintent
2828
2929
3030def save_coinify_callback (request , order ):
@@ -51,55 +51,49 @@ def save_coinify_callback(request, order):
5151 return callbackobject
5252
5353
54- def coinify_api_request (api_method , order , ** kwargs ):
55- # Initiate coinify API
56- coinifyapi = CoinifyAPI (settings .COINIFY_API_KEY , settings .COINIFY_API_SECRET )
57-
58- # is this a supported method?
59- if not hasattr (coinifyapi , api_method ):
60- logger .error ("coinify api method not supported" % api_method )
61- return False
62-
63- # get and run the API call using the SDK
64- method = getattr (coinifyapi , api_method )
65-
66- # catch requests exceptions as described in https://github.com/CoinifySoftware/python-sdk#catching-errors and
67- # http://docs.python-requests.org/en/latest/user/quickstart/#errors-and-exceptions
54+ def coinify_api_request (api_method , order , payload ):
55+ url = f"{ settings .COINIFY_API_URL } { api_method } "
56+ headers = {
57+ "accept" : "application/json" ,
58+ "content-type" : "application/json" ,
59+ "X-API-KEY" : settings .COINIFY_API_KEY ,
60+ }
6861 try :
69- response = method ( ** kwargs )
62+ response = requests . post ( url , data = json . dumps ( payload ), headers = headers )
7063 except requests .exceptions .RequestException as E :
7164 logger .error ("requests exception during coinify api request: %s" % E )
7265 return False
7366
67+ logger .error (response .text )
7468 # save this API request to the database
7569 req = CoinifyAPIRequest .objects .create (
7670 order = order ,
7771 method = api_method ,
78- payload = kwargs ,
79- response = response ,
72+ payload = payload ,
73+ response = response . json () ,
8074 )
8175 logger .debug ("saved coinify api request %s in db" % req .id )
8276
8377 return req
8478
8579
8680def handle_coinify_api_response (apireq , order , request ):
87- if apireq .method == "invoice_create" or apireq . method == "invoice_get " :
81+ if apireq .method == "payment-intents " :
8882 # Parse api response
89- if apireq .response [ "success" ] :
90- # save this new coinify invoice to the DB
91- coinifyinvoice = process_coinify_invoice_json (
92- invoicejson = apireq .response [ "data" ] ,
83+ if "paymentWindowUrl" in apireq .response :
84+ # save this new coinify intent to the DB
85+ coinifyintent = process_coinify_payment_intent_json (
86+ intentjson = apireq .response ,
9387 order = order ,
9488 request = request ,
9589 )
96- return coinifyinvoice
90+ return coinifyintent
9791 else :
98- api_error = apireq .response [ "error" ]
92+ api_error = apireq .json ()
9993 logger .error (
10094 "coinify API error: {} ({})" .format (
101- api_error ["message " ],
102- api_error ["code " ],
95+ api_error ["errorMessage " ],
96+ api_error ["errorCode " ],
10397 ),
10498 )
10599 return False
@@ -112,36 +106,26 @@ def handle_coinify_api_response(apireq, order, request):
112106# API CALLS
113107
114108
115- def get_coinify_invoice (coinify_invoiceid , order , request ):
116- # put args for API request together
117- invoicedict = {"invoice_id" : coinify_invoiceid }
118-
119- # perform the api request
120- apireq = coinify_api_request (api_method = "invoice_get" , order = order , ** invoicedict )
121-
122- coinifyinvoice = handle_coinify_api_response (apireq , order , request )
123- return coinifyinvoice
124-
125-
126- def create_coinify_invoice (order , request ):
109+ def create_coinify_payment_intent (order , request ):
127110 # put args for API request together
128- invoicedict = {
111+ intentdict = {
129112 "amount" : float (order .total ),
130113 "currency" : "DKK" ,
131- "plugin_name" : "BornHack webshop" ,
132- "plugin_version" : "1.0" ,
133- "description" : "BornHack order id #%s" % order .id ,
134- "callback_url" : order .get_coinify_callback_url (request ),
135- "return_url" : order .get_coinify_thanks_url (request ),
136- "cancel_url" : order .get_cancel_url (request ),
114+ "pluginIdentifier" : "BornHack webshop" ,
115+ "orderId" : order .id ,
116+ "customerId" : "bbca76fa-1337-439a-ae29-a3c2c2c84c4b" ,
117+ "customerEmail" : "coinifycustomer@bornhack.example" ,
118+ "memo" : "BornHack order id #%s" % order .id ,
119+ "successUrl" : order .get_coinify_thanks_url (request ),
120+ "failureUrl" : order .get_cancel_url (request ),
137121 }
138122
139123 # perform the API request
140124 apireq = coinify_api_request (
141- api_method = "invoice_create " ,
125+ api_method = "payment-intents " ,
142126 order = order ,
143- ** invoicedict ,
127+ payload = intentdict ,
144128 )
145129
146- coinifyinvoice = handle_coinify_api_response (apireq , order , request )
147- return coinifyinvoice
130+ coinifyintent = handle_coinify_api_response (apireq , order , request )
131+ return coinifyintent
0 commit comments