def create(self, validated_data): user = validated_data.pop('user') recipient = self._get_recipient(validated_data, user) self._update_contact_method(validated_data, user) exchange_rate = get_current_exchange_rate() service_fees = get_current_service_fees() service_fee = None for fee in service_fees: if fee.service == p.BILL: service_fee = fee break transaction = self.Meta.model.objects.create( sender=user, exchange_rate=exchange_rate, service_fee=service_fee, recipient=recipient, **validated_data) amount_usd = round_amount(transaction.amount_ghs / transaction.exchange_rate.usd_ghs) transaction.amount_usd = round_amount(amount_usd) transaction.service_charge = round_amount( amount_usd * transaction.service_fee.percentual_fee + transaction.service_fee.fixed_fee) self._initial_values(transaction) return transaction
def create(self, validated_data): user = validated_data.pop('user') recipient = self._get_recipient(validated_data, user) self._update_contact_method(validated_data, user) exchange_rate = get_current_exchange_rate() service_fees = get_current_service_fees() service_fee = None for fee in service_fees: if fee.service == p.BILL: service_fee = fee break transaction = self.Meta.model.objects.create( sender=user, exchange_rate=exchange_rate, service_fee=service_fee, recipient=recipient, **validated_data ) amount_usd = round_amount( transaction.amount_ghs / transaction.exchange_rate.usd_ghs) transaction.amount_usd = round_amount(amount_usd) transaction.service_charge = round_amount( amount_usd * transaction.service_fee.percentual_fee + transaction.service_fee.fixed_fee) self._initial_values(transaction) return transaction
def get(self, request, *args, **kwargs): response_dict = {} try: exchange_rate = get_current_exchange_rate() response_dict['exchange_rate_id'] = exchange_rate.id response_dict['usd_ghs'] = exchange_rate.usd_ghs service_fees = get_current_service_fees() response_dict['airtime'] = {} response_dict['bill'] = {} for service_fee in service_fees: if service_fee.service == AIRTIME: response_dict['airtime']['service_fee_id'] = service_fee.id response_dict['airtime']['fixed_fee'] = service_fee.fixed_fee response_dict['airtime']['percentual_fee'] = service_fee.percentual_fee elif service_fee.service == BILL: response_dict['bill']['service_fee_id'] = service_fee.id response_dict['bill']['fixed_fee'] = service_fee.fixed_fee response_dict['bill']['percentual_fee'] = service_fee.percentual_fee except ObjectDoesNotExist: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) except ObjectsDoNotExist: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(response_dict)
def save_model(self, request, obj, form, change): if 'state' in form.changed_data: obj.add_status_change(author=request.user, comment=getattr(obj, 'state')) if 'amount_ghs' in form.changed_data and getattr(obj, 'amount_ghs'): obj.exchange_rate = get_current_exchange_rate() obj.amount_usd = getattr(obj, 'amount_ghs') / obj.exchange_rate.usd_ghs elif 'amount_usd' in form.changed_data and getattr(obj, 'amount_usd'): obj.exchange_rate = get_current_exchange_rate() obj.amount_ghs = getattr(obj, 'amount_usd') * obj.exchange_rate.usd_ghs obj.save()
def save(self, *args, **kwargs): with dbtransaction.atomic(): self.object.pricing = get_current_pricing(self.site) self.object.exchange_rate = get_current_exchange_rate() self.object.generate_reference_number() self.object.received_amount = self.object.pricing.calculate_received_amount( self.object.sent_amount, self.object.receiving_country) self.object.recipient.save() self.object.recipient = models.Recipient.objects.get(id=self.object.recipient.id) self.object.save() return models.Transaction.objects.get(id=self.object.id)
def get(self, request, *args, **kwargs): user = self.request.user referral = None exchange_rate = {} airtime_service_fee = None bill_service_fee = None try: referral = self.request.user.referral except ObjectDoesNotExist: referral = create_referral_code(self.request.user) try: exchange_rate = get_current_exchange_rate() service_fees = get_current_service_fees() if service_fees[0].service == p.AIRTIME: airtime_service_fee = service_fees[0] bill_service_fee = service_fees[1] else: airtime_service_fee = service_fees[1] bill_service_fee = service_fees[0] except Exception: exchange_rate = {} airtime_service_fee = None bill_service_fee = None insta_pay = {} insta_pay['user'] = UserSerializer(user).data if exchange_rate: insta_pay['pricing'] = ExchangeRateSerializer(exchange_rate).data insta_pay['pricing']['exchange_rate_id'] = insta_pay['pricing'][ 'id'] insta_pay['pricing']['airtime'] = ServiceFeeSerializer( airtime_service_fee).data insta_pay['pricing']['bill'] = ServiceFeeSerializer( bill_service_fee).data insta_pay['referral'] = ReferralSerializer(referral).data return Response(insta_pay)
def check_parameters(self, request): super(CreateInstantPayment, self).check_parameters(request) amount_ghs = request.data.get('amount_ghs', None) exchange_rate_id = request.data.get('exchange_rate_id', None) service_fee_id = request.data.get('service_fee_id', None) if not amount_ghs or not exchange_rate_id or not service_fee_id: raise APIException(constants.INVALID_PARAMETERS) # check if Exchange Rate or Service Charge has expired service_fees = get_current_service_fees() service_fee = None for fee in service_fees: if fee.service == p.AIRTIME: service_fee = fee break if (get_current_exchange_rate().id != exchange_rate_id or service_fee.id != service_fee_id): raise APIException(constants.PRICING_EXPIRED)
def post(self, request): # block countries we are not licensed to operate in and tor clients client_ip = get_client_ip(request) if country_blocked(request, client_ip) or is_tor_node(client_ip): return Response(status=HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS) try: site = get_site_by_request(self.request) serializer = self.serializer_class(user=request.user, site=site, data=request.DATA) if serializer.is_valid(): # check if Pricing or Exchange Rate has expired if get_current_pricing(site).id != request.DATA.get('pricing_id') or\ get_current_exchange_rate().id != request.DATA.get('exchange_rate_id'): return Response({'detail': constants.PRICING_EXPIRED}, status=status.HTTP_400_BAD_REQUEST) # basic profile information incomplete if not request.user.profile.information_complete: return Response({'detail': constants.PROFILE_INCOMPLETE}, status=status.HTTP_400_BAD_REQUEST) # calculate today's transaction volume for the user todays_vol = request.user.profile.todays_transaction_volume( site, request.DATA.get('sent_amount')) # sender has exceeded basic transaction limit if todays_vol > get_current_limit(site).user_limit_basic: # sender has exceeded maximum daily transaction limit if todays_vol > get_current_limit(site).user_limit_complete: return Response({'detail': constants.TRANSACTION_LIMIT_EXCEEDED}, status=status.HTTP_400_BAD_REQUEST) # sender has not provided additional document if not request.user.profile.documents_provided: return Response({'detail': constants.ADDITIONAL_DOCUMENTS_MISSING}, status=status.HTTP_400_BAD_REQUEST) # documents still await verification if not request.user.profile.documents_verified: return Response({'detail': constants.DOCUMENTS_NOT_VERIFIED}, status=status.HTTP_400_BAD_REQUEST) self.object = serializer.save(force_insert=True) self.post_save(self.object, created=True) return Response( {'invoice_id': self.invoice_id, 'received_amount': self.object.received_amount, 'received_currency': self.object.received_currency, 'operation_mode': get_current_state(site).state}, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except APIException: self.object.set_invalid() return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) except AccountException: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)