Skip to content

Commit

Permalink
Merge pull request #248 from maykinmedia/develop
Browse files Browse the repository at this point in the history
Adyen 10+ support
  • Loading branch information
alextreme authored Jan 22, 2025
2 parents aaa86fc + 288d7c1 commit f96711b
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 25 deletions.
2 changes: 2 additions & 0 deletions djadyen/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LIVE_URL_PREFIX_ERROR = "Please provide the live_url_prefix. "
"https://docs.adyen.com/development-resources/live-endpoints#live-url-prefix"
13 changes: 12 additions & 1 deletion djadyen/management/commands/sync_payment_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from djadyen import settings

from ...constants import LIVE_URL_PREFIX_ERROR
from ...models import AdyenIssuer, AdyenPaymentOption


Expand Down Expand Up @@ -38,17 +39,27 @@ class Command(BaseCommand):
def handle(self, *args, **options):
ady = Adyen.Adyen()

if not settings.DJADYEN_ENVIRONMENT:
assert False, "Please provide an environment."

if (
settings.DJADYEN_ENVIRONMENT == "live"
and not settings.DJADYEN_LIVE_URL_PREFIX
):
assert False, LIVE_URL_PREFIX_ERROR

# Setting global values
ady.payment.client.platform = settings.DJADYEN_ENVIRONMENT
ady.payment.client.xapikey = settings.DJADYEN_SERVER_KEY
ady.payment.client.app_name = settings.DJADYEN_APPNAME
ady.payment.client.live_endpoint_prefix = settings.DJADYEN_LIVE_URL_PREFIX

# Setting request data.
request = {
"merchantAccount": settings.DJADYEN_MERCHANT_ACCOUNT,
}
# Starting the checkout.
result = ady.checkout.payment_methods(request)
result = ady.checkout.payments_api.payment_methods(request)

payment_methods = result.message.get("paymentMethods")
for payment_method in payment_methods:
Expand Down
3 changes: 2 additions & 1 deletion djadyen/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

DJADYEN_SERVER_KEY = getattr(dj_settings, "DJADYEN_SERVER_KEY")
DJADYEN_CLIENT_KEY = getattr(dj_settings, "DJADYEN_CLIENT_KEY")
DJADYEN_ENVIRONMENT = getattr(dj_settings, "DJADYEN_ENVIRONMENT", "test")
DJADYEN_ENVIRONMENT = getattr(dj_settings, "DJADYEN_ENVIRONMENT")
DJADYEN_APPNAME = getattr(dj_settings, "DJADYEN_APPNAME", "Djadyen Payment")
DJADYEN_MERCHANT_ACCOUNT = getattr(dj_settings, "DJADYEN_MERCHANT_ACCOUNT")
DJADYEN_CURRENCYCODE = getattr(dj_settings, "DJADYEN_CURRENCYCODE", "EUR")
Expand All @@ -15,3 +15,4 @@
DJADYEN_DEFAULT_COUNTRY_CODE = getattr(
dj_settings, "DJADYEN_DEFAULT_COUNTRY_CODE", "nl"
)
DJADYEN_LIVE_URL_PREFIX = getattr(dj_settings, "DJADYEN_LIVE_URL_PREFIX", None)
31 changes: 24 additions & 7 deletions djadyen/templatetags/adyen_tags.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging

from django import template
from django.utils.translation import get_language

import Adyen

from djadyen import settings
from djadyen.choices import Status
from djadyen.constants import LIVE_URL_PREFIX_ERROR

register = template.Library()
logger = logging.getLogger("adyen")
Expand All @@ -24,10 +26,18 @@ def adyen_payment_component(
logger.info("Start new payment for {}".format(str(order.reference)))
ady = Adyen.Adyen()

if not settings.DJADYEN_ENVIRONMENT:
assert False, "Please provide an environment."

if settings.DJADYEN_ENVIRONMENT == "live" and not settings.DJADYEN_LIVE_URL_PREFIX:
assert False, LIVE_URL_PREFIX_ERROR

# Setting global values
ady.payment.client.platform = settings.DJADYEN_ENVIRONMENT
ady.payment.client.xapikey = settings.DJADYEN_SERVER_KEY
ady.payment.client.app_name = settings.DJADYEN_APPNAME
ady.payment.client.live_endpoint_prefix = settings.DJADYEN_LIVE_URL_PREFIX

# Setting request data.
request = {
"amount": {
Expand All @@ -38,13 +48,20 @@ def adyen_payment_component(
"merchantAccount": merchant_account,
"returnUrl": order.get_return_url(),
"shopperLocale": language.lower(),
"countryCode": country_code.lower()
if country_code
else settings.DJADYEN_DEFAULT_COUNTRY_CODE,
"countryCode": (
country_code.lower()
if country_code
else settings.DJADYEN_DEFAULT_COUNTRY_CODE
),
}
try:
request["shopperEmail"] = order.email
except Exception:
pass

logger.info(request)
# Starting the checkout.
result = ady.checkout.sessions(request)
result = ady.checkout.payments_api.sessions(request)

if result.status_code == 201:
return {
Expand All @@ -54,9 +71,9 @@ def adyen_payment_component(
"environment": settings.DJADYEN_ENVIRONMENT,
"redirect_url": order.get_return_url,
"language": get_language(),
"payment_type": order.payment_option.adyen_name
if order.payment_option
else "",
"payment_type": (
order.payment_option.adyen_name if order.payment_option else ""
),
"issuer": order.issuer.adyen_id if order.issuer else "",
}
return {}
Expand Down
26 changes: 23 additions & 3 deletions djadyen/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging

from django.http import JsonResponse, HttpResponseRedirect
from django.http import HttpResponseRedirect, JsonResponse
from django.views.generic.detail import DetailView

import Adyen

from djadyen import settings
from djadyen.choices import Status
from djadyen.constants import LIVE_URL_PREFIX_ERROR

logger = logging.getLogger("adyen")

Expand Down Expand Up @@ -41,25 +42,44 @@ def get(self, request, *args, **kwargs):
resultRedirect = request.GET.get("redirectResult")
if resultRedirect:
ady = Adyen.Adyen()

if not settings.DJADYEN_ENVIRONMENT:
assert False, "Please provide an environment."

if (
settings.DJADYEN_ENVIRONMENT == "live"
and not settings.DJADYEN_LIVE_URL_PREFIX
):
assert False, LIVE_URL_PREFIX_ERROR

# Setting global values
ady.payment.client.platform = settings.DJADYEN_ENVIRONMENT
ady.payment.client.xapikey = settings.DJADYEN_SERVER_KEY
ady.payment.client.app_name = settings.DJADYEN_APPNAME
ady.payment.client.live_endpoint_prefix = settings.DJADYEN_LIVE_URL_PREFIX

# Setting request data.
request = {
"details": {
"redirectResult": resultRedirect,
},
}
# Requesting the status.
result = ady.checkout.payments_details(request)
if result.message.get("resultCode") == "Authorised":
result = ady.checkout.payments_api.payments_details(request)
result_code = result.message.get("resultCode")
if result_code == "Authorised":
self.handle_authorised(self.object)
elif result_code != "Pending":
self.handle_error(self.object)
return super(AdyenResponseView, self).get(request, *args, **kwargs)

def handle_authorised(self, order):
raise NotImplementedError()

def handle_error(self, order):
order.status = Status.Error
order.save()


class AdyenOrderStatusView(DetailView):
slug_field = "reference"
Expand Down
4 changes: 0 additions & 4 deletions javascript/djadyen.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ document.addEventListener("DOMContentLoaded", async () => {
sessionData: config.dataset.sessionData,
},
onPaymentCompleted: (result) => {
console.log("onPaymentCompleted", "result", result);
console.log("onPaymentCompleted", "checkout", checkout);
console.log("onPaymentCompleted", "component", component);

window.location = config.dataset.redirectUrl;
},
onError: (error, component) => {
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# see http://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files
[metadata]
name = djadyen
version = 2.0.4
version = 2.0.6
description = Django adyen payment integration
long_description = file: README.rst
url = https://github.com/maykinmedia/djadyen
Expand Down Expand Up @@ -31,7 +31,7 @@ zip_safe = False
include_package_data = True
packages = find:
install_requires =
Adyen>=6.0.0<7.0.0
Adyen>=10.0.0
django>=1.10
django-choices
requests
Expand Down
5 changes: 4 additions & 1 deletion testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@
]

# New settings
DJADYEN_ENVIRONMENT = "test"
DJADYEN_SERVER_KEY = "test_server_key"
DJADYEN_CLIENT_KEY = "test_client_key"
DJADYEN_APPNAME = "Djadyen testapp"
DJADYEN_MERCHANT_ACCOUNT = "Djadyen_merchant_account"
DJADYEN_ORDER_MODELS = ["testapp.Order"]
DJADYEN_NOTIFICATION_KEY = "3424242342353453422435626342654643645624564526436435643542364365"
DJADYEN_NOTIFICATION_KEY = (
"3424242342353453422435626342654643645624564526436435643542364365"
)
2 changes: 1 addition & 1 deletion testapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try:
from django.urls import path, include
from django.urls import include, path
except Exception:
from django.conf.urls import url as path, include

Expand Down
2 changes: 0 additions & 2 deletions testapp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.views.generic import TemplateView

from djadyen.choices import Status
from djadyen.views import AdyenResponseView

Expand Down
1 change: 0 additions & 1 deletion tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from djadyen.choices import Status
from djadyen.models import AdyenIssuer, AdyenNotification, AdyenPaymentOption

from testapp.models import Order


Expand Down
4 changes: 2 additions & 2 deletions tests/test_management_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SyncPaymentMethods(TestFileMixin, TestCase):
@requests_mock.mock()
def test_on_empty_database_mock(self, mock):
mock.post(
"https://checkout-test.adyen.com/v69/paymentMethods",
"https://checkout-test.adyen.com/v71/paymentMethods",
[
{
"content": self._get_test_file("payment_methods.json").read(),
Expand All @@ -37,7 +37,7 @@ def test_on_empty_database_mock(self, mock):
@requests_mock.mock()
def test_on_existing_database_mock(self, mock):
mock.post(
"https://checkout-test.adyen.com/v69/paymentMethods",
"https://checkout-test.adyen.com/v71/paymentMethods",
[
{
"content": self._get_test_file("payment_methods.json").read(),
Expand Down
1 change: 1 addition & 0 deletions tests/test_notification_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json

from django.urls import reverse

from django_webtest import WebTest
Expand Down

0 comments on commit f96711b

Please sign in to comment.