Beispiel #1
0
def append_buy_shipping_form(request):
    exhibit = get_object_or_404(Exhibit,
                                pk=request.GET.get('exhibit_id'),
                                last_bidder_member=request.user)
    transaction = Transaction()
    transaction.exhibit = exhibit
    transaction.user = request.user
    form = BuyShippingForm(instance=transaction)

    return render(request, 'payment/modal_buy_shipping.html', {
        'exhibit': exhibit,
        'form': form
    })
Beispiel #2
0
def append_buy_now_form(request):
    if request.user.is_authenticated():
        item = get_object_or_404(Item, pk=request.GET.get('item_pk'))
        transaction = Transaction()
        transaction.item = item
        transaction.user = request.user
        form = BuyNowForm(instance=transaction)

        return render(request, 'payment/modal_buy_now.html', {
            'item': item,
            'form': form
        })
    else:
        return HttpResponse('')
Beispiel #3
0
def append_bids_return_form(request):

    try:
        exhibit = Exhibit.objects.ended()\
            .exclude(last_bidder_member=request.user) \
            .exclude(id__in=[payment.exhibit_id for payment in request.user.payments.processing_item_transaction().all() if payment.exhibit_id]) \
            .extra(select={'refund_time_left': 'FLOOR({}-(UNIX_TIMESTAMP()-ended_unixtime))'.format(settings.BID_REFUND_TIME)}) \
            .extra(where=['UNIX_TIMESTAMP() - ended_unixtime < {}'.format(settings.BID_REFUND_TIME)]) \
            .annotate(bid_refund=Count('id')) \
            .select_related('item') \
            .get(bids__user=request.user, id=request.GET.get('exhibit_id'))
    except Exhibit.DoesNotExist:
        raise Http404

    transaction = Transaction()
    transaction.exhibit = exhibit
    transaction.user = request.user
    form = BuyWithBidsReturnForm(instance=transaction)

    return render(request, 'payment/modal_buy_and_return_bids.html', {
        'exhibit': exhibit,
        'form': form
    })
Beispiel #4
0
    def post(self, request):
        name = request.POST.get('name')
        email = request.POST.get('email')
        amount = request.POST.get('amount')
        transaction = Transaction()
        transaction.amount = amount
        transaction.email = email
        transaction.name = name
        transaction.save()

        request_result = ws_proxy.request_payment(
            settings.MERCHANT_ID, settings.MERCHANT_PASSWORD, amount,
            '1Hes Donation', name, '09121231231', email, transaction.id,
            settings.SERVER_BASE_ADDRESS + '/payment/result/' +
            str(transaction.id))
        if request_result.ResultStatus == 'Succeed':
            return HttpResponseRedirect(request_result.PaymentPath)
Beispiel #5
0
def PaymentPage(request,goalId=None):
    if request.method=='POST':  
        form=PaymentForm(request.POST)
        if form.is_valid():
            goal=Goal.objects.get(goalId=base64.urlsafe_b64decode(str(goalId)))
            
            sponsor=Sponsor()
            sponsor.name=form.cleaned_data['name']
            sponsor.emailId=form.cleaned_data['email']        
            sponsor.save()
            
            transaction=Transaction()
            transaction.sponsor=sponsor
            transaction.goal=goal
            transaction.amount_per_lb = form.cleaned_data['amtperlb']
            transaction.require_refund = not(form.cleaned_data['payToCharity'])
            #transaction.status = True
            transaction.save()
            
            return HttpResponseRedirect('/paypal/' + base64.urlsafe_b64encode(str(transaction.transaction_id)))
        else:  #display the form when not valid
            return render_to_response('PaymentPage.html',{'form':form}, context_instance=RequestContext(request))
    else:   
        '''blank Form'''
        ''' Check if the goal still active'''
        try:
            Goal.objects.get(goalId=base64.urlsafe_b64decode(str(goalId)),status='inprogress')
        except Goal.DoesNotExist:
            context={'is_Active':True}
            return render_to_response('PaymentPage.html',context, context_instance=RequestContext(request))
        form=PaymentForm()
        goal=Goal.objects.get(goalId=base64.urlsafe_b64decode(str(goalId)))
        name=goal.weightloser.name
        loseWeight=goal.current_weight-goal.target_weight
        period=goal.deadline-goal.creation_date
        period=period.days
        charity=goal.charity.name

        context={'form':form,'name':name,'loseWeight':loseWeight,'period':period,'charity':charity}
        return render_to_response('PaymentPage.html',context, context_instance=RequestContext(request))
Beispiel #6
0
    def post(self, request):
        name = request.POST.get('name')
        email = request.POST.get('email')
        amount = request.POST.get('amount')
        transaction = Transaction()
        transaction.amount = amount
        transaction.email = email
        transaction.name = name
        transaction.save()

        request_result = ws_proxy.request_payment(settings.MERCHANT_ID, settings.MERCHANT_PASSWORD, amount,
                                                  '1Hes Donation', name,
                                                  '09121231231', email, transaction.id,
                                                  settings.SERVER_BASE_ADDRESS + '/payment/result/' + str(transaction.id))
        if request_result.ResultStatus == 'Succeed':
            return HttpResponseRedirect(request_result.PaymentPath)
Beispiel #7
0
def buy_now(request):
    transaction = Transaction(user=request.user,
                              ip=request.META.get('HTTP_X_REAL_IP')
                              or request.META.get('REMOTE_ADDR'))
    form = BuyNowForm(request.POST, instance=transaction)

    if form.is_valid():
        transaction = form.save(commit=False)
        transaction.type = 'buy_item'
        transaction.amount = transaction.item.price

        if transaction.shipping_method == 'standard':
            transaction.amount += transaction.item.standard_shipping_price
        else:
            transaction.amount += transaction.item.priority_shipping_price

        transaction.save()

        if transaction.payment_method == 'paypal':

            paypalrestsdk.configure({
                'mode': PAYPAL_MODE,
                'client_id': PAYPAL_CLIENT_ID,
                'client_secret': PAYPAL_SECRET
            })

            payment = paypalrestsdk.Payment({
                "intent":
                "sale",
                "redirect_urls": {
                    "return_url": PAYPAL_BUY_NOW_RETURN_URL,
                    "cancel_url": PAYPAL_BUY_NOW_CANCEL_URL
                },
                "payer": {
                    "payment_method": "paypal",
                },
                "transactions": [{
                    "item_list": {
                        "items": [
                            {
                                "name": transaction.item.name,
                                "sku": transaction.item.code,
                                "price": str(transaction.item.price),
                                "currency": "USD",
                                "quantity": 1
                            },
                            {
                                "name":
                                "Shipping",
                                "sku":
                                transaction.shipping_method,
                                "price":
                                str(transaction.amount -
                                    transaction.item.price),
                                "currency":
                                "USD",
                                "quantity":
                                1
                            },
                        ]
                    },
                    "amount": {
                        "total": str(transaction.amount),
                        "currency": "USD"
                    },
                    "description":
                    "This is the payment transaction description."
                }]
            })

            if payment.create():
                print("Payment %s created successfully" % payment.id)
                for link in payment.links:
                    if link.method == "REDIRECT":
                        redirect_url = link.href
                        print("Redirect for approval: %s" % redirect_url)
                        request.user.last_payment_id = payment.id
                        request.user.last_transaction_id = transaction.id
                        request.user.save()
                        # can't make here direct redirect or redirect via js 'next' due to facebook
                        callback_js = "$('#buy-now-form').attr('action', '%s'); " \
                                      "$('#buy-now').removeClass('ajax-submit');" \
                                      "$('#buy-now').text('redirecting...');" \
                                      "$('#buy-now').attr('type', 'submit');" \
                                      "$('#buy-now').click();" % redirect_url

                        return HttpResponse(
                            json.dumps({
                                'result': 'success',
                                'callback_js': callback_js
                            }))
            else:
                print(payment.error)
            # return HttpResponse(json.dumps({'result': 'success', 'next': '/checkout/review/{}/'.format(order.id)}))
        else:
            pass
    else:
        print form._errors
        response = {}
        for k in form.errors:
            response[k] = form.errors[k][0]
        print response
        return HttpResponse(
            json.dumps({
                'response': response,
                'result': 'error'
            }))
Beispiel #8
0
def buy_bids(request):

    bids_cost = int(request.POST.get('bids_cost', 0))
    exhibit_pk = int(request.POST.get('exhibit_id', 0))
    coupon_code = int(request.POST.get('coupon_code_hidden', 0) or 0)
    coupon = None
    amount_for_fund = bids_cost
    use_funding_credits = request.POST.get('use_funding_credits')

    # if was used funding credits snd all ok with don't redirect to paypal
    if use_funding_credits:
        try:
            funding_credits = int(request.POST.get('funding_credits', 0))
        except ValueError:
            return HttpResponse(
                json.dumps(
                    dict(result='error',
                         response={
                             'funding_credits': 'funding amount is not valid'
                         })))

        if funding_credits > request.user.funding_credits:
            return HttpResponse(
                json.dumps(
                    dict(result='error',
                         response={
                             'funding_credits': 'not enough funding credits'
                         })))

        exhibit = Exhibit.objects.get(pk=exhibit_pk)
        exhibit.bonus_fund(request.user, funding_credits)
        request.user.save()
        websocket_api(action='FUND_ITEM', params=exhibit_pk, request=request)
        return HttpResponse(json.dumps({'result': 'success'}))

    if coupon_code:
        try:
            coupon = Coupon.objects.get(pk=coupon_code)
        except (Coupon.DoesNotExist, ValueError):
            return HttpResponse(
                json.dumps(
                    dict(result='error',
                         response={'coupon_code_hidden': 'wrong code'})))

        if coupon.is_expired():
            return HttpResponse(
                json.dumps(
                    dict(result='error',
                         response={'coupon_code_hidden':
                                   'coupon is expired'})))

        if coupon.is_already_used_by(request.user):
            return HttpResponse(
                json.dumps(
                    dict(result='error',
                         response={
                             'coupon_code_hidden':
                             'coupon code can be used only once'
                         })))

        if coupon.min_package_amount and coupon.min_package_amount > bids_cost:
            return HttpResponse(
                json.dumps(
                    dict(
                        result='error',
                        response={
                            'coupon_code_hidden':
                            'coupon can be used only for $%s and more packages'
                            % coupon.min_package_amount
                        })))

        amount_for_fund = bids_cost * coupon.funding_percent / 100

    if not bids_cost or not exhibit_pk:
        return HttpResponse(json.dumps({'result': 'error'}))

    bids_number = Fund.get_bids_count_by_cost(bids_cost, coupon)

    transaction = Transaction(user=request.user,
                              amount=bids_cost,
                              type='buy_bids',
                              coupon=coupon,
                              amount_for_fund=amount_for_fund,
                              ip=request.META.get('HTTP_X_REAL_IP')
                              or request.META.get('REMOTE_ADDR'),
                              exhibit_id=exhibit_pk)

    transaction.save()

    if transaction.payment_method == 'paypal':

        paypalrestsdk.configure({
            'mode': PAYPAL_MODE,
            'client_id': PAYPAL_CLIENT_ID,
            'client_secret': PAYPAL_SECRET
        })

        payment = paypalrestsdk.Payment({
            "intent":
            "sale",
            "redirect_urls": {
                "return_url": PAYPAL_BUY_BIDS_RETURN_URL,
                "cancel_url": PAYPAL_BUY_BIDS_CANCEL_URL
            },
            "payer": {
                "payment_method": "paypal",
            },
            "transactions": [{
                "item_list": {
                    "items": [
                        {
                            "name": '%s bids' % bids_number,
                            "sku": '%s bids' % bids_number,
                            "price": str(bids_cost),
                            "currency": "USD",
                            "quantity": 1
                        },
                    ]
                },
                "amount": {
                    "total": str(bids_cost),
                    "currency": "USD"
                },
                "description":
                "This is the payment transaction description."
            }]
        })

        if payment.create():
            print("Payment %s created successfully" % payment.id)
            for link in payment.links:
                if link.method == "REDIRECT":
                    redirect_url = link.href
                    print("Redirect for approval: %s" % redirect_url)
                    request.user.last_payment_id = payment.id
                    request.user.last_transaction_id = transaction.id
                    request.user.save()
                    # can't make here direct redirect or redirect via js 'next' due to facebook
                    callback_js = "$('#fund-exhibit-form').attr('action', '%s'); " \
                                  "$('#fund-exhibit').removeClass('ajax-submit');" \
                                  "$('#fund-exhibit').text('redirecting...');" \
                                  "$('#fund-exhibit').attr('type', 'submit');" \
                                  "$('#fund-exhibit').click();" % redirect_url
                    return HttpResponse(
                        json.dumps({
                            'result': 'success',
                            'callback_js': callback_js
                        }))
        else:
            print(payment.error)
            return HttpResponseRedirect('/')

        # return HttpResponse(json.dumps({'result': 'success', 'next': '/checkout/review/{}/'.format(order.id)}))
    else:
        pass