def post(self, request): # Create order body = parse_body(request) user = User.objects.get(id=body['user']) products = [{ "instance": Product.objects.get(id=product["id"]), "options": product["options"], } for product in body["products"]] order = Order(user=user, ) order.save() for product in products: order.products.add(product["instance"]) payment = Payment( order=order, method='Credit card', value=self.sum_products_prices(products), ) payment.save() charge = stripe.Charge.create( amount=self.format_price(self.sum_products_prices(products)), currency='usd', description='Order #' + str(order.id), customer=user.creditcard.customer_id, ) response = {"id": order.id, "total": payment.value} return JsonResponse(response, status=201)
def add_payment(): persons = Person.objects.all() payments = PaymentType.objects.all() payers = Payer.objects.all() for person in persons: # С вероятностью 10% он ничего не получает if random.randint(0, 10) >= 9: continue # А тут получает for i in range(random.randint(10, 30)): pay = Payment() pay.person = person pay.payment_sum = random.randint(500, 10000) pay.payer = random.choice(payers) pay.payment_type = random.choice(payments) pay.payment_date = datetime.datetime(year=random.choice( (2015, 2016, 2017, 2018)), month=random.randint(1, 12), day=random.randint(1, 27)) pay.save()
def payments(loan_id): request_data = request.get_json() if re.match(loan_id_re, loan_id) is None: response = jsonify({"error": "Specified loan id is not valid"}) response.status_code = 400 return response try: validate(request_data, schemas["payment"]) except ValidationError as validation_error: return generate_error_message(validation_error.message, 400) date = ciso8601.parse_datetime(request_data["date"]) if date is None: return generate_error_message("Specified date doesn't follow the ISO8601 norm", 400) if date.tzinfo is None or date.tzinfo.utcoffset(date) is None: date = date.replace(tzinfo=pytz.UTC) db_loan = Loan.get_loan(loan_id) if db_loan is None: return generate_error_message("Specified loan id doesn't exist", 400) db_loan_installment = Loan.calculate_loan_payment(db_loan.rate, db_loan.term, db_loan.amount) if request_data["amount"] != db_loan_installment: return generate_error_message( "Specified amount is not equal to monthly installment of {}".format(db_loan_installment), 400) db_loan_payments = Payment.get_loan_payments(loan_id) if len(db_loan_payments) != 0: made_payments = [] for payment in db_loan_payments: if payment.payment == "made": made_payments.append(payment) if len(made_payments) == db_loan.term: return generate_error_message("This loan has already been paid", 409) db_loan_last_payment = db_loan_payments[-1] months = relativedelta.relativedelta(date, db_loan_last_payment.date).months if abs(months) < 1: return generate_error_message( "Payments work in a monthly base. Since last payment {} one month hasn't passed".format( db_loan_last_payment.date), 409) request_data = request.get_json() payment = request_data["payment"] amount = request_data["amount"] payment = Payment(id=str(uuid.uuid4()), loan_id=loan_id, payment=payment, date=date, amount=amount) payment.save() response = jsonify({"message": "Payment successful"}) response.status_code = 201 return response