def checkout_page(request): cart_obj, new_obj = Cart.objects.new_or_get(request) user = request.user status = False if pay.objects.filter(user=user, STATUS='TXN_SUCCESS'): trns = pay.objects.filter(user=user, STATUS='TXN_SUCCESS')[0] status = True if request.user.is_authenticated: if request.method == "POST": name = request.POST.get('name', '') phone = request.POST.get('phone', '') email = request.POST.get('email', '') address = request.POST.get( 'address1', '') + " " + request.POST.get('address2', '') city = request.POST.get('city', '') state = request.POST.get('state', '') zip_code = request.POST.get('zip_code', '') amount = cart_obj.total order = Order(name=name, phone=phone, email=email, address=address, city=city, state=state, zip_code=zip_code) order.save() # Request paytm to transfer the amount to your account after payment by user param_dict = { 'MID': 'QNsvNs85056236851568', 'ORDER_ID': str(order.order_id), 'TXN_AMOUNT': str(amount), 'CUST_ID': email, 'INDUSTRY_TYPE_ID': 'Retail', 'WEBSITE': 'WEBSTAGING', 'CHANNEL_ID': 'WEB', 'CALLBACK_URL': 'http://127.0.0.1:8000/handlerequest', } param_dict['CHECKSUMHASH'] = Checksum.generate_checksum( param_dict, MERCHANT_KEY) return render(request, 'paytm.html', { 'param_dict': param_dict, 'user': user }) return render(request, 'checkout.html', { 'cart': cart_obj, 'status': status }) else: return redirect('user:user_login')
def create_order(): if not request.form: return abort(422) req = request.form jreq = json.dumps(req).encode("utf8") test = req['quantite'] print(test) if req["quantite"] == '' or int(req['quantite']) < 1: return abort(422, 'la création d\'une commande nécessite un produit') if (not Products.get_or_none(Products.id == req['id'])): return abort(422, 'Le produit demandé n\'est pas en inventaire') Order.create(product_id=req['id'], quantite=req['quantite']) return redirect('/'), 302
def order(order_id): get_order = Order.get_or_none(Order.id == order_id) if not order: return abort(404) prod = Products.get_or_none(Products.id == get_order.product_id) total_price = prod.price * get_order.quantite if (prod.weight < 500): shipping_price = 5 elif prod.weight < 2000: shipping_price = 10 else: shipping_price = 25 order_json = { "order": { "id": get_order.id, "total_price": total_price, "email": null, "credit_card": {}, "shipping_information": {}, "paid": false, "transaction": {}, "product": { "id": get_order.product_id, "quantity": get_order.quantite }, "shipping_price": shipping_price } } data_order = json.dumps(order_json) order_data = json.loads(data_order) for product in order_data['order']: prod_id = product['id'] prod_quant = product['quantity'] order = order_data['order'] return views.view_order(order, prod_id, prod_quant)
def cabin_visit(product_data, user): # Create an order with a cabin settlement product order = Order( amount=0, # Default to 0 and update to correct amount later language_code=product_data['språk'], ) if not user.is_anonymous(): order.user = user order.save() # Save to DB in order to create relations, even though we'll have to do an UPDATE later cabin_settlement = CabinSettlement(order=order) if 'brukerdata' in product_data: cabin_settlement.name = product_data['brukerdata']['navn'] cabin_settlement.email = product_data['brukerdata']['epost'] cabin_settlement.save() amount = 0 for cabin in product_data['hytter']: sted = Sted.get(cabin['id']) # @TODO: Use private fields to get the appropriate owner: first forening of privat.juridisk_eier, # privat.vedlikeholdes_av forening = Forening.objects.get(turbasen_object_id=sted['grupper'][0]) cabin_visit = CabinVisit( cabin_settlement=cabin_settlement, cabin_object_id=sted['_id'], ) cabin_visit.save() for losji in cabin['losji']: # Business rule: Anyone claiming to be a member will get the membership price pays_membership_price = losji['ermedlem'] lodging = Lodging.objects.get( id=losji['id'], forening=forening.id, ) if losji['antall'] < 0: raise PermissionDenied # Try to relate this visitor to a user user = None memberid_unrecognized = None if losji['medlemsnummer'] is not None: try: user = User.get_or_create_inactive(memberid=losji['medlemsnummer']) # Note that this is where we could verify that the member has paid their annual fee, but it is # currently ignored except User.DoesNotExist: # We received an unrecognized memberid, but we'll still allow the purchase, so save the given # memberid for future reference, whatever it is memberid_unrecognized = losji['medlemsnummer'] # Save the visitor details cabin_visitor = CabinVisitor( cabin_visit=cabin_visit, protocol_number=losji['protokollnummer'], user=user, memberid_unrecognized=memberid_unrecognized, lodging_type=lodging.name, price=lodging.get_price(pays_membership_price), nights=losji['antall'], paid_membership_price=pays_membership_price, ) cabin_visitor.save() # Now calculate the lodging price and add it to the total amount amount += lodging.get_price(pays_membership_price) * losji['antall'] for proviant in cabin['proviant']: # Business rule: The client decides whether or not supplies should have membership price pays_membership_price = product_data['medlemspris'] supply = Supply.objects.get( id=proviant['id'], supply_category__forening=forening.id, ) if proviant['antall'] < 0: raise PermissionDenied # Save a copy of the current products and prices supply_purchase = SupplyPurchase( cabin_visit=cabin_visit, category=supply.supply_category.name, name=supply.name, price=supply.get_price(pays_membership_price), count=proviant['antall'], paid_membership_price=pays_membership_price, ) supply_purchase.save() # Now calculate the supplies price and add it to the total amount amount += supply.get_price(pays_membership_price) * proviant['antall'] # Save the total amount in øre amount *= 100 order.amount = amount order.save() return order
def test_order(self): order = Order() order.id = 1 order.create_order_no() self.assertTrue(re.match(r'^\d+T\d+$', order.order_no))
def create_transaction(request): """This view is called by the phone app to initiate a new transaction""" if request.method != 'POST' or 'data' not in request.POST: raise PermissionDenied transaction = json.loads(request.POST['data']) product = transaction.get('product', '') if transaction['ticket'] is not None and request.user.is_anonymous(): # Ticket operations require that the user is authenticated return HttpResponseBadRequest(json.dumps({ 'errors': [{ 'message': 'Cannot apply ticket operation when user is not authenticated', 'code': error_codes.TICKET_OPERATION_WHILE_NOT_AUTHENTICATED, }] })) if transaction['ticket'] == 'kun_lagre': order = Order( ticket_transaction=True, amount=0, user=request.user, language_code=transaction['språk'], ) order.save() elif product == 'cabin_visit': order = cabin_visit(transaction, request.user) else: return HttpResponseBadRequest(json.dumps({ 'errors': [{ 'message': "Unknown product '%s'" % product, 'code': error_codes.USER_HAS_NO_TICKET, }] })) cancel_return_url = 'https://%s%s' % ( request.site.domain, reverse('payment:postmessage_callback'), ) accept_return_url = 'https://%s%s' % ( request.site.domain, reverse('payment:postmessage_callback'), ) server_callback_url = 'https://%s%s' % ( request.site.domain, reverse('payment:payment_provider_callback'), ) input_parameters = { 'merchant': settings.DIBS_MERCHANT_ID, 'orderId': order.get_order_id(), 'amount': order.amount, # The sum is provided in øre 'currency': 'NOK', # ISO 4217 'captureNow': 1, 'acceptReturnUrl': accept_return_url, 'cancelReturnUrl': cancel_return_url, 'callbackUrl': server_callback_url, 'language': transaction['språk'], } # # Ticket (stored payment card) handling # if transaction['ticket'] is not None: if transaction['ticket'] == 'lagre': # User uses card, but wants to save their ticket input_parameters['createTicketAndAuth'] = 1 elif transaction['ticket'] == 'bruk_lagret': # User claims to have a saved ticket if request.user.dibs_payment_ticket is None: # They thought they did, but they don't return HttpResponseBadRequest(json.dumps({ 'errors': [{ 'message': 'User has no ticket stored', 'code': error_codes.USER_HAS_NO_TICKET, }] })) # We'll perform the DIBS server-to-server call here and return the response to the client try: dibs_response, order, payment_accepted = dibs.authorize_ticket(request, order) if payment_accepted and not order.ticket_transaction: order.try_to_send_email_receipt() return HttpResponse(json.dumps(dibs_response)) except: # Downstream handles exception logging, so just return an error message return { 'errors': [{ 'message': 'Internal error during ticket authorization, please try again', 'code': error_codes.INTERNAL_SERVER_ERROR, }] } elif transaction['ticket'] == 'kun_lagre': # Just create and save a new ticket; no transaction input_parameters['createTicket'] = 1 # Force amount to 1 since DIBS doesn't allow 0 even though it is irrelevant in this situation input_parameters['amount'] = 1 del input_parameters['captureNow'] # # End ticket handling # input_parameters['MAC'] = dibs.calculate_hmac(input_parameters) return HttpResponse(json.dumps(input_parameters))