def order_detail(request, token): order = get_object_or_404(Order, token=token) user = request.user if order.user and user != order.user and not user.is_superuser: return render_403(request) payments = Payment.objects.filter(order=order) templates = [] if order.kind: part = '/'.join(order.kind.lower().split('.')) templates.append('froide_payment/order/%s/detail.html' % part) templates.append('froide_payment/order/default.html') domain_object = order.get_domain_object() result = request.GET.get('result') if result == 'success': any_confirmed = any( payment.status == PaymentStatus.CONFIRMED for payment in payments ) if not any_confirmed: for payment in payments: provider = provider_factory(payment.variant) if hasattr(provider, 'update_status'): provider.update_status(payment) ctx = { 'payments': payments, 'order': order, 'object': domain_object, 'result': result } return render(request, templates, ctx)
def create_plan(self, data): metadata = self.get_payment_metadata(data) provider = provider_factory(data['payment_method']) plan = provider.get_or_create_plan(metadata['plan_name'], metadata['category'], data['amount'], data['interval']) return plan
def convert_lastschrift_to_sepa(self, request, payment_id): from payments.core import provider_factory from .utils import lastschrift_sepa_mail SEPA = 'sepa' provider = provider_factory(SEPA) if lastschrift_sepa_mail is None: raise PermissionDenied if not request.method == 'POST': raise PermissionDenied if not self.has_change_permission(request): raise PermissionDenied try: payment = Payment.objects.get(id=payment_id) except Payment.DoesNotExist: return redirect('/') order = payment.order owner_name = '' iban = '' try: owner_name = payment.attrs.owner iban = payment.attrs.iban except KeyError: if order.customer: customer_data = order.customer.data owner_name = customer_data.get('owner', order.get_full_name()) iban = customer_data.get('iban', '') # Mark this payment as CANCELED again => not happening anymore payment.status = PaymentStatus.CANCELED payment.save() if order.is_recurring: subscription = order.subscription plan = subscription.plan if plan.provider != SEPA: new_plan = provider.get_or_create_plan(plan.name, plan.category, plan.amount, plan.interval) subscription.plan = new_plan subscription.save() new_payment = order.get_or_create_payment(SEPA, request=request) new_payment.attrs.iban = iban new_payment.attrs.owner = owner_name new_payment.save() lastschrift_sepa_mail.send(email=payment.billing_email, context={ 'first_name': payment.billing_first_name, 'order': order }, priority=True) return redirect(order.get_absolute_payment_url(SEPA))
def __init__(self, data, service_type): self.data = data self.service_type = service_type self.payment_id = self.data.get('payment_id', None) self.provider = provider_factory('click') self.payment = get_object_or_404(get_payment_model(), id=self.payment_id) super(Services, self).__init__(self.provider, self.payment, self.data)
def test_hidden_fields(settings): settings.LIVE_PAYMENT_HOST = 'example.com' payment = Payment.objects.create( variant='paymaster', total='1234.50', currency='RUB', ) provider: PaymasterProvider = provider_factory(payment.variant) hidden_data = provider.get_hidden_fields(payment) assert hidden_data['PAYMENT_TOKEN'] == payment.token assert settings.LIVE_PAYMENT_HOST in hidden_data['LMI_SUCCESS_URL']
def process_data(request, token, provider=None): ''' Calls process_data of an appropriate provider. Raises Http404 if variant does not exist. ''' Payment = get_payment_model() payment = get_object_or_404(Payment, token=token) if not provider: try: provider = provider_factory(payment.variant) except ValueError: raise Http404('No such payment') return provider.process_data(payment, request)
def static_callback(request, variant): try: provider = provider_factory(variant) except ValueError: raise Http404('No such provider') if hasattr(provider, 'handle_webhook'): return provider.handle_webhook(request) token = provider.get_token_from_request(request=request, payment=None) if token is False: # This callback is not meant for this endpoint return HttpResponse(status=204) if not token: raise Http404('Invalid response') return process_data(request, token, provider)
def order_success(request, token): order = get_object_or_404(Order, token=token) user = request.user if not can_access(order, user): return redirect('/') payments = Payment.objects.filter(order=order) any_confirmed = any(payment.status == PaymentStatus.CONFIRMED for payment in payments) if not any_confirmed: for payment in payments: provider = provider_factory(payment.variant) if hasattr(provider, 'update_status'): provider.update_status(payment) return redirect(order.get_success_url())
def order_detail(request, token): order = get_object_or_404(Order, token=token) user = request.user if 'json' in request.META.get('HTTP_ACCEPT', ''): return JsonResponse({'name': order.get_full_name()}) if not can_access(order, user): return redirect('/') payments = Payment.objects.filter(order=order) templates = [] if order.kind: part = '/'.join(order.kind.lower().split('.')) templates.append('froide_payment/order/%s/detail.html' % part) templates.append('froide_payment/order/default.html') domain_object = order.get_domain_object() result = request.GET.get('result') if result == 'success': any_confirmed = any(payment.status == PaymentStatus.CONFIRMED for payment in payments) if not any_confirmed: for payment in payments: provider = provider_factory(payment.variant) if hasattr(provider, 'update_status'): provider.update_status(payment) ctx = { 'payments': payments, 'order': order, 'object': domain_object, 'result': result } return render(request, templates, ctx)
def test_provider_factory(self): core.provider_factory('default')
def test_provider_factory(self): core.provider_factory('default')
def get_provider(self): return provider_factory(self.plan.provider)
def test_provider_factory(): assert isinstance(provider_factory('razorpay'), RazorPayProvider)