Skip to content

Commit

Permalink
Merge pull request #142 from unicef/refund_action
Browse files Browse the repository at this point in the history
superusers mass refund
  • Loading branch information
domdinicola authored Feb 4, 2025
2 parents 2ceae95 + 4d4c511 commit 8b1bc0f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/hope_payment_gateway/apps/core/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf import settings
from django.contrib.auth.models import User


def set_superusers(user: User | None = None, is_new: bool = False, **kwargs: dict) -> dict:
if user and is_new and user.email in settings.SUPERUSERS:
user.is_superuser = True
user.save()
return {}
64 changes: 64 additions & 0 deletions src/hope_payment_gateway/apps/gateway/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.admin import helpers
from django.http import HttpResponse, StreamingHttpResponse
from django.shortcuts import redirect, render
from django.template import Context, Template
from django.utils import dateformat
from django.utils.encoding import smart_str
Expand All @@ -20,9 +22,27 @@
from constance import config

from hope_payment_gateway.apps.fsp.moneygram.tasks import moneygram_update
from hope_payment_gateway.apps.fsp.western_union.api.client import WesternUnionClient
from hope_payment_gateway.apps.gateway.templatetags.payment import clean_value


class ActionForm(forms.Form):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
action = forms.CharField(label="", required=True, initial="", widget=forms.HiddenInput())


class RefundForm(ActionForm):
REFUND_CHOICES = (
("INCORRECT_AMT", "Incorrect Amount"),
("TECH_PROB", "Technical Problem"),
("DUP_TRAN", "Duplicate Transaction"),
("NO_PICKUP", "No Pickup Available"),
("WRONG_CRNCY", "Wrong Country"),
("WRONG_CNTRY", "Wrong Currency"),
)
reason = forms.ChoiceField(choices=REFUND_CHOICES)


class TemplateExportForm(CSVConfigForm):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
select_across = forms.BooleanField(
Expand Down Expand Up @@ -173,4 +193,48 @@ def moneygram_update_status(modeladmin, request, queryset):
moneygram_update(qs.values_list("id", flat=True))


def moneygram_refund(modeladmin, request, queryset):
qs = queryset.filter(parent__fsp__vendor_number=config.MONEYGRAM_VENDOR_NUMBER)
moneygram_update(qs.values_list("id", flat=True))

opts = modeladmin.model._meta
perm = f"{opts.app_label}.can_cancel_transaction"
if not request.user.has_perm(perm):
messages.error(request, _("Sorry you do not have rights to execute this action"))
return None

initial = {
"_selected_action": request.POST.getlist(helpers.ACTION_CHECKBOX_NAME),
"select_across": request.POST.get("select_across") == "1",
"action": "moneygram_refund",
}

if "apply" in request.POST:
form = RefundForm(request.POST, initial=initial)
if form.is_valid():
reason = form.cleaned_data["reason"]
for obj in qs:
obj.payload.update({"refuse_reason_code": reason})
obj.save()
WesternUnionClient().refund(obj.fsp_code, obj.extra_data)
messages.info(request, _(f"Updating {qs.count()}"))
return redirect("admin:gateway_paymentrecord_changelist")

initial.update({"_validate": 1})

form = RefundForm(initial=initial)
admin_form = helpers.AdminForm(form, modeladmin.get_fieldsets(request), {}, [], model_admin=modeladmin)
ctx = {
"title": "MoneyGram: Refund",
"opts": opts,
"app_label": modeladmin.model._meta.app_label,
"form": form,
"selection": queryset,
"adminform": admin_form,
}
ctx.update(modeladmin.admin_site.each_context(request))
return render(request, "admin/gateway/refund.html", ctx)


moneygram_update_status.short_description = "MoneyGram: update status"
moneygram_refund.short_description = "MoneyGram: mass refund"
5 changes: 3 additions & 2 deletions src/hope_payment_gateway/apps/gateway/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
export_as_template,
export_as_template_impl,
moneygram_update_status,
moneygram_refund,
)
from hope_payment_gateway.apps.gateway.models import (
DeliveryMechanism,
Expand Down Expand Up @@ -80,7 +81,7 @@ class PaymentRecordAdmin(ExtraButtonsMixin, AdminFiltersMixin, admin.ModelAdmin)
}
raw_id_fields = ("parent",)

actions = [export_as_template, moneygram_update_status]
actions = [export_as_template, moneygram_update_status, moneygram_refund]

def get_queryset(self, request: HttpRequest) -> QuerySet:
return super().get_queryset(request).select_related("parent__fsp")
Expand Down Expand Up @@ -337,7 +338,7 @@ def mg_refund(self, request: HttpRequest, pk: int) -> TemplateResponse:
try:
resp = MoneyGramClient().refund(obj.fsp_code, obj.get_payload())
return self.handle_mg_response(request, resp, pk, "Refund")
except InvalidTokenError as e:
except (InvalidTokenError, KeyError) as e:
logger.error(e)
self.message_user(request, str(e), messages.ERROR)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "admin_extra_buttons/action_page.html" %}
{% block action-content %}
<form action="" method="post" id="refund-form">{% csrf_token %}
{% csrf_token %}
<table>
{{ adminform.form }}
</table>
<input type="submit" name="apply" value="Update">
<input type="button" value="Back" onclick="location.href='.'">

</form>
{% endblock action-content %}
1 change: 1 addition & 0 deletions src/hope_payment_gateway/config/fragments/social_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"social_core.pipeline.social_auth.associate_user",
"social_core.pipeline.social_auth.load_extra_data",
"social_core.pipeline.user.user_details",
"hope_payment_gateway.apps.social.pipeline.set_superusers",
)

USER_FIELDS = ["username", "email", "first_name", "last_name"]
Expand Down

0 comments on commit 8b1bc0f

Please sign in to comment.