def get(self, request, *args, **kwargs): serializer = serializers.RequestTransactionViewGet( data=request.query_params) if serializer.is_valid(): transaction = Transaction.get(serializer.validated_data['ref_num']) if transaction: return Response( serializers.Transaction(transaction, many=False).data, status.HTTP_200_OK) else: temp_transaction = cache.get( "transaction:%s" % (serializer.validated_data['ref_num'])) transaction = None if temp_transaction is not None: transaction = json.loads(temp_transaction) if transaction['application'] == request.application.id: if transaction['state'] == 'pay': transaction['status'] = 'pending' return Response(transaction, status=status.HTTP_200_OK) return Response({"message": "No transaction found."}, status=status.HTTP_404_NOT_FOUND) else: return Response({"message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
def transaction_payment_view(request, ref_num): if request.method == "GET": context = {} config = Config.get() temp_transaction = cache.get("transaction:%s" % (ref_num)) url = '%s/transaction/payment/%s' % (settings.BASE_URL, ref_num) transaction = None if temp_transaction is not None: transaction = json.loads(temp_transaction) context = transaction context['state'] = 'pay' if 'url' in transaction and transaction['url']: return HttpResponseRedirect(transaction['url']) else: user = User.get_user(username=transaction['username']) payer = get_payer_name(user) gateway = None gateways = [] if 'gateway' in transaction and transaction['gateway']: gateway = Gateway.objects.filter( id=transaction['gateway']).first() else: gateways = Gateway.objects.filter( currency=transaction['currency']).all() amount_str = None if transaction['amount']: amount_str = humanizeAmount(transaction['amount']) context = { 'state': 'pay', 'amount': amount_str, 'amount_value': transaction['amount'], 'gateway': gateway, 'gateways': gateways, 'user': user, 'note': transaction.get('note', None), 'background_color': config.pay_background_color, 'url': url, 'relative_url': url.replace(settings.BASE_URL, ""), # 'form': transaction.form, 'currency': transaction['currency'].upper(), 'payer': payer, 'title': transaction['title'], 'logo': transaction['logo'], } return render(request, 'transaction/pay.html', context) else: transaction = Transaction.get(ref_num=ref_num) if transaction is None: context = { 'state': 'not_found', 'background_color': config.error_background_color, } return render(request, 'transaction/pay.html', context) # print(transaction) payer = get_payer_name(transaction.user) background_color = config.paid_background_color, if transaction.status == 'rejected': background_color = config.error_background_color, context = { 'state': transaction.status, 'amount': humanizeAmount(transaction.amount), 'gateway': transaction.gateway, 'user': transaction.user, 'background_color': background_color[0], 'form': transaction.form, 'currency': transaction.currency.upper(), 'trace_no': persianiser('%s-%d' % (transaction.random, transaction.id)), 'url': url, 'relative_url': url.replace(settings.BASE_URL, ""), 'note': transaction.note, 'date': getJalaliDate(transaction.created_at), 'payer': payer, } if transaction.application: context['title'] = transaction.application.title if transaction.application.logo: context['logo'] = transaction.application.logo.url if transaction.form: context['title'] = transaction.form.title if transaction.form.logo: context['logo'] = transaction.form.logo.url if 'continue_url' in transaction.details: context['continue_url'] = transaction.details['continue_url'] return render(request, 'transaction/pay.html', context) else: temp_transaction = cache.get("transaction:%s" % (ref_num)) url = '%s/transaction/payment/%s' % (settings.BASE_URL, ref_num) transaction = None config = Config.get() if temp_transaction is not None: transaction = json.loads(temp_transaction) amount = request.data.get('amount', None) gateway_id = request.data.get('gateway', None) user = User.get_user(username=transaction['username']) payer = get_payer_name(user) gateway = None gateways = [] if 'gateway' in transaction and transaction['gateway']: gateway = Gateway.objects.filter( id=transaction['gateway']).first() else: gateways = Gateway.objects.filter( currency=transaction['currency']).all() amount_str = None if transaction['amount']: amount_str = humanizeAmount(transaction['amount']) context = { 'state': 'pay', 'amount': amount_str, 'amount_value': transaction['amount'], 'gateway': gateway, 'gateways': gateways, 'background_color': config.pay_background_color, 'note': transaction.get('note', None), 'user': user, 'url': url, 'relative_url': url.replace(settings.BASE_URL, ""), # 'form': transaction.form, 'currency': transaction['currency'].upper(), 'payer': payer, 'title': transaction['title'], 'logo': transaction['logo'], } if amount is None or not amount: context['message'] = _('Please enter a valid amount.') return render(request, 'transaction/pay.html', context) if gateway_id is None: context['message'] = _('Please choose a gateway.') return render(request, 'transaction/pay.html', context) if int(amount) < settings.MIN_PAYMENT_AMOUNT: context['message'] = _( 'The minimum acceptable price is %(amount) %(currency)' ) % { 'amount': persianiser(humanizeAmount(settings.MIN_PAYMENT_AMOUNT)), 'currency': transaction['currency'].upper() } return render(request, 'transaction/pay.html', context) if int(amount) > settings.MAX_PAYMENT_AMOUNT: context['message'] = _( 'The maximum acceptable price is %(amount) %(currency)' ) % { 'amount': persianiser(humanizeAmount(settings.MAX_PAYMENT_AMOUNT)), 'currency': transaction['currency'].upper() } return render(request, 'transaction/pay.html', context) gateways_query = Gateway.list(gateway_id, ) if amount: amount = int(amount) transaction = Transaction.update_request( ref_num, amount=amount, gateways=gateways_query, ) if 'url' in transaction and transaction['url']: return HttpResponseRedirect(transaction['url']) else: url = '%s/transaction/payment/%s' % (settings.BASE_URL, ref_num) return HttpResponseRedirect(url) else: context = { 'state': 'not_found', 'background_color': config.error_background_color, } return render(request, 'transaction/pay.html', context)
def delete(self, request, pk, format=None): transaction = Transaction.get(pk) transaction.delete() return Response(status=status.HTTP_204_NO_CONTENT)