Esempio n. 1
0
def payment(request):
    if request.method == 'POST':
        form = OrderForm(request.POST)
        print(form.errors)
        if form.is_valid():
            this_order = form.save(commit=False)
            cart = Cart(request.session).cart_serializable
            payment_method = request.POST['payment-method']
            this_order.user = request.user
            if request.POST.get('shipping-options') == 'shipping':
                this_order.is_shipping = True
            this_order.save()
            order_number = this_order.pk
            for v in cart.values():
                order_detail = OrderDetail()
                order_detail.good = Goods.objects.get(pk=v['product_pk'])
                order_detail.count = v['quantity']
                order_detail.order = this_order
                order_detail.save()

            if payment_method == 'paypal':
                # paypal
                this_order.payment_method = 'p'
                this_order.save()
                if len(this_order.orderdetail_set.all()) > 1:
                    item_name = this_order.orderdetail_set.all()[0].good.name \
                                + this_order.orderdetail_set.all()[1].good.name + '...'
                else:
                    try:
                        item_name = this_order.orderdetail_set.all(
                        )[0].good.name
                    except IndexError:
                        return JsonResponse({
                            'message':
                            'Your order does NOT contain any goods.\n'
                            'Add some goods and try again!',
                            'redirect':
                            reverse('shop:shopping-cart-show')
                        })

                paypal_dict = {
                    "business":
                    "{}".format(settings.PAYPAL_ID),
                    "amount":
                    "{}".format(this_order.total_price),
                    "item_name":
                    item_name,
                    "invoice":
                    "{}".format(this_order.uuid),
                    "notify_url":
                    settings.PAYPAL_URL + reverse('paypal-ipn'),
                    "return_url":
                    settings.PAYPAL_URL + '/shop/thankyou/' +
                    str(this_order.uuid),
                    "cancel_return":
                    settings.PAYPAL_URL + reverse('shop:shop_main'),
                    "custom":
                    "{}".format(this_order.user)
                }
                paypal_form = PayPalPaymentsForm(initial=paypal_dict).render()
                send_gmail(send_to=str(this_order.user.email),
                           subject='IRKSHOP: Thankyou for your Order!',
                           order=this_order)
                # clear cart
                Cart(request.session).clear()
                return JsonResponse({
                    'message':
                    "Sucessfully Ordered!\n"
                    "We've send you your order mail.\n"
                    "Please Continue with Paypal Payment.\n"
                    "(Paypal Checkout will show soon.)",
                    'paypal-form':
                    paypal_form
                })
            elif payment_method == 'bank-transfer':
                # currency = requests.get('http://www.floatrates.com/daily/usd.json').text
                # today_usd_to_krw = int(json.loads(currency)['krw']['rate'] / 10) * 10
                this_order = Order.objects.get(pk=order_number)
                today_usd_to_krw = 1000
                this_order.payment_method = 'b'
                this_order.usd_to_krw = today_usd_to_krw
                this_order.bank_transfer_name = request.POST.get(
                    'bank_transfer_name')
                this_order.save()
                send_gmail(
                    send_to=str(this_order.user.email),
                    subject=
                    'IRKSHOP: Please Proceed Bank Transfer to finish your purchase',
                    order=this_order)
                # clear cart
                Cart(request.session).clear()
                return JsonResponse({
                    'message':
                    "Sucessfully Ordered!\n"
                    "Please Continue with Bank Transfer.\n"
                    "We've mailed you our invoice.",
                    'redirect':
                    '/shop/bank_payment/' + str(this_order.uuid) + '/'
                })
        else:
            return JsonResponse({
                'message':
                'Order Form is not fully filled!\n'
                'NOTE: Ingress Email and Agent Name are Required'
            })
    else:
        form = OrderForm()
    currency = requests.get('http://www.floatrates.com/daily/usd.json').text
    today_usd_to_krw = int(json.loads(currency)['krw']['rate'] / 10) * 10
    return render(request, 'payment/payment.html', {
        'form': form,
        'today_usd_to_krw': today_usd_to_krw
    })
Esempio n. 2
0
def payment_local(request):
    if request.method == 'POST':
        form = OrderForm(request.POST)
        print(form.is_valid())
        if form.is_valid():
            print(form.cleaned_data)
            this_order = Order()
            cart = Cart(request.session).cart_serializable
            this_order.address = form.cleaned_data.get('address', '')
            this_order.additional_address = form.cleaned_data.get(
                'AdditionalAddress', '')
            this_order.custom_order = form.cleaned_data.get('OrderOptioin', '')

            this_order.user = request.user
            this_order.save()
            order_number = this_order.pk
            for v in cart.values():
                order_detail = OrderDetail()
                order_detail.good = Goods.objects.get(pk=v['product_pk'])
                order_detail.count = v['quantity']
                order_detail.order = this_order
                order_detail.save()

            # clear cart
            Cart(request.session).clear()

            # paypal
            this_order = Order.objects.get(pk=order_number)

            if this_order.address != None:
                shipping_fee_order = OrderDetail()
                # TODO: Change Hard Coding this pk to env?
                shipping_fee_order.good = Goods.objects.get(pk=10)
                shipping_fee_order.count = 1
                shipping_fee_order.order = this_order
                shipping_fee_order.save()

            total_price = this_order.total_price
            try:
                paypal_dict = {
                    "business": "{}".format(settings.PAYPAL_ID),
                    "amount": "{}".format(total_price),
                    "item_name":
                    "{}".format(this_order.orderdetail.all()[0].good),
                    "invoice": "{}".format(this_order.pk),
                    # TODO: Change URL from code to Envs.json
                    "notify_url": "http://shop.resist.kr/paypal/",
                    "return_url": "http://shop.resist.kr/thankyou/",
                    "cancel_return": "http://shop.resist.kr/",
                    "custom": "{}".format(this_order.user)
                }
            except:
                return JsonResponse(
                    {'message': 'please checkout with more than 1 items'})
            paypal_form = PayPalPaymentsForm(initial=paypal_dict).render()

            orders = this_order.orderdetail.all()
            orders_detail = ''
            for order in orders:
                orders_detail += ('{} x {}\n'.format(order.good, order.count))

            send_mail(
                user=settings.GMAIL_ID,
                pwd=settings.GMAIL_PW,
                recipient=User.objects.get(username=this_order.user).email,
                subject="Order to SHOPIRK: Via Noir Seoul",
                body=
                "Hello {},\n We've Just got your order from SHOPIRK: Via Noir Seoul.\nThis is how you've ordered, Please check carefully.\n"
                "Invoice Number: #{}\n"
                "YOUR ORDERS:\n"
                "{}\n"
                "Thanks again for your Order.\n"
                "Sincerely, IRK.".format(this_order.user, this_order.pk,
                                         orders_detail),
            )

            return JsonResponse({
                'message':
                "Sucessfully Ordered!\n"
                "Please Continue with Paypal Payment.\n"
                "(Paypal Checkout will show soon.)",
                'paypal-form':
                paypal_form
            })
        else:
            return JsonResponse({'message': 'form is not Valid'})
    else:
        form = OrderForm()

    return render(request, 'payment/payment_local.html', {
        'form': form,
    })