Example #1
0
def process_payment_request(amount, currency, token, payerid):
    """
    utility function to perform PayPal payment
    """
    # call PayPal driver and perform the relevant PayPal API method to charge the money
    driver = PayPal()
    result = driver.DoExpressCheckoutPayment(currency=currency,
                                             amount=str(amount),
                                             token=token,
                                             payerid=payerid)

    # persist the response to the db with PayPalResponse instance
    paypal_response = PayPalResponse()
    paypal_response.fill_from_response(driver.GetPaymentResponse())
    paypal_response.status = PayPalResponse.get_default_status()

    if result:
        # Amount charged successfully.
        paypal_response.payment_received = True
        paypal_response.save()
        return True, paypal_response
    else:
        # Amount charged could not be charged successfully. Note that
        # the paypal_response is going to be saved.
        paypal_response.error = _(driver.doexpresscheckoutpaymenterror)
        paypal_response.save()
        return False, paypal_response
Example #2
0
def process_refund_request(response, amount):
    """
    utility function to perform PayPal refund
    """

    # call PayPal driver and perform the relevant PayPal API method to refund the money
    driver = PayPal()
    result = driver.RefundTransaction(response.trans_id,
                                      refundtype="Partial",
                                      amount=str(amount),
                                      currency=response.currencycode)

    # persist the response to the db with PayPalResponse instance
    paypal_response = PayPalResponse()
    paypal_response.fill_from_response(driver.GetRefundResponse(),
                                       action="Refund")
    paypal_response.status = PayPalResponse.get_cancel_status()

    if result:
        # Amount refunded successfully.
        paypal_response.payment_received = True
        paypal_response.save()
        return True, paypal_response
    else:
        # Amount charged could not be refunded successfully. Note that
        # the paypal_response is going to be saved
        paypal_response.error = _(driver.refundtransactionerror)
        paypal_response.save()
        return False, paypal_response
def setcheckout(request, return_url, cancel_url, error_url, template = "paypal/setcheckout.html", currency = "USD"):
  
    if request.POST:
        # normalize the given amount
        amount = request.POST.get("amount")
        try:
            amount = Decimal(amount)
            amount = str(amount.quantize(Decimal(".01"), rounding = ROUND_UP))
        except:
            if request.user.is_authenticated():
                request.user.message_set.create(message = _("No given valid amount. Please check the amount that will be charged."))
            return HttpResponseRedirect(error_url)

        # call the PayPal driver (2)
        driver = PayPal()
        # call the relevant API method (3)
        result = driver.SetExpressCheckout(amount, currency, return_url, cancel_url)
        # perform the response (4)
        if not result:
            print driver.apierror
            # show the error message (comes from PayPal API) to the user and redirect him/her to the error page
            if request.user.is_authenticated():
                request.user.message_set.create(message = _(driver.setexpresscheckouterror))
            return HttpResponseRedirect(error_url)
        
        # send him/her to the PayPal website to check his/her order details out
        redirect_url = driver.paypal_url()
        return HttpResponseRedirect(redirect_url)
    
    return render_to_response(template,
                              {'currency': currency,
                               'return_url': return_url,
                               'cancel_url': cancel_url,
                               'error_url' : error_url,
                               }, context_instance = RequestContext(request))
Example #4
0
def set_checkout(request,
                 amount,
                 return_url,
                 cancel_url,
                 error_url,
                 template="paypal/setcheckout.html",
                 currency="USD",
                 **kwargs):
    """
    Django view to process PayPal SetExpressCheckout API call.
    If response 'Success' or 'SuccessWithWarning' comes,
        redirects user to the PayPal website to continue checkout process.
    If response 'Failed' or 'FailedWithWarning' comes,
        shows the error and redirects user to the 'payment page' to choose another POS or PayPal again.
    ROUTINES
      1) Perform and validate POST data
      2) Call Paypal driver
      3) Execute the relevant method
      4) Accroding to the method response, redirect user to the given urls
    """

    if request.POST:
        # normalize the given amount
        # amount = request.POST.get("amount")
        try:
            amount = Decimal(amount)
            amount = str(amount.quantize(Decimal(".01"), rounding=ROUND_UP))
        except InvalidOperation:
            if request.user.is_authenticated():
                messages.error(
                    request,
                    _("No given valid amount. "
                      "Please check the amount that will be charged."))
            return HttpResponseRedirect(error_url)

        # call the PayPal driver (2)
        driver = PayPal()
        # call the relevant API method (3)
        result = driver.SetExpressCheckout(amount, currency, return_url,
                                           cancel_url, **kwargs)
        # perform the response (4)
        if not result:
            print driver.apierror
            # show the error message (comes from PayPal API) to the user and redirect him/her to the error page
            if request.user.is_authenticated():
                messages.info(request, _(driver.setexpresscheckouterror))
            return HttpResponseRedirect(error_url)

        # send him/her to the PayPal website to check his/her order details out
        redirect_url = driver.paypal_url()
        return HttpResponseRedirect(redirect_url)

    return render_to_response(template, {
        'currency': currency,
        'return_url': return_url,
        'cancel_url': cancel_url,
        'error_url': error_url,
    },
                              context_instance=RequestContext(request))
Example #5
0
def set_checkout(request, amount, return_url, cancel_url, error_url, template="paypal/setcheckout.html", currency="USD",
                 **kwargs):
    """
    Django view to process PayPal SetExpressCheckout API call.
    If response 'Success' or 'SuccessWithWarning' comes,
        redirects user to the PayPal website to continue checkout process.
    If response 'Failed' or 'FailedWithWarning' comes,
        shows the error and redirects user to the 'payment page' to choose another POS or PayPal again.
    ROUTINES
      1) Perform and validate POST data
      2) Call Paypal driver
      3) Execute the relevant method
      4) Accroding to the method response, redirect user to the given urls
    """

    if request.POST:
        # normalize the given amount
        # amount = request.POST.get("amount")
        try:
            amount = Decimal(amount)
            amount = str(amount.quantize(Decimal(".01"), rounding=ROUND_UP))
        except InvalidOperation:
            if request.user.is_authenticated():
                messages.error(request, _("No given valid amount. "
                                          "Please check the amount that will be charged."))
            return HttpResponseRedirect(error_url)

        # call the PayPal driver (2)
        driver = PayPal()
        # call the relevant API method (3)
        result = driver.SetExpressCheckout(amount, currency, return_url, cancel_url, **kwargs)
        # perform the response (4)
        if not result:
            print driver.apierror
            # show the error message (comes from PayPal API) to the user and redirect him/her to the error page
            if request.user.is_authenticated():
                messages.info(request, _(driver.setexpresscheckouterror))
            return HttpResponseRedirect(error_url)

        # send him/her to the PayPal website to check his/her order details out
        redirect_url = driver.paypal_url()
        return HttpResponseRedirect(redirect_url)

    return render_to_response(template,
                              {'currency': currency,
                               'return_url': return_url,
                               'cancel_url': cancel_url,
                               'error_url': error_url,
                               }, context_instance=RequestContext(request))
Example #6
0
def setcheckout(request, return_url, cancel_url, error_url, template = "paypal/setcheckout.html", currency = "USD"):
    """
    Django view to process PayPal SetExpressCheckout API call.
    If response 'Success' or 'SuccessWithWarning' comes, redirects user to the PayPal website to continue checkout process.
    If response 'Failed' or 'FailedWithWarning' comes, shows the error and redirects user to the 'payment page' to choose another POS or PayPal again.
    """
    
    #############################################################################
    # ROUTINES                                                                  #
    # 1) Perform and validate POST data                                         #
    # 2) Call Paypal driver                                                     #
    # 3) Execute the relevant method                                            #
    # 4) Accroding to the method response, redirect user to the given urls      #
    #############################################################################

    if request.POST:
        # normalize the given amount
        amount = request.POST.get("amount")
        try:
            amount = Decimal(amount)
            amount = str(amount.quantize(Decimal(".01"), rounding = ROUND_UP))
        except:
            if request.user.is_authenticated():
                request.user.message_set.create(message = _("No given valid amount. Please check the amount that will be charged."))
            return HttpResponseRedirect(error_url)

        num_cart_items = request.POST.get('num_cart_items', None)
        cart_items = None
        if num_cart_items:
            cart_items = []
            for i in range(0, int(num_cart_items)):
                item = {
                    'NAME':   request.POST.get('cart_items[%s][NAME]' % i),
                    'NUMBER': request.POST.get('cart_items[%s][NUMBER]' % i),
                    'DESC':   request.POST.get('cart_items[%s][DESC]' % i),
                    'AMT':    request.POST.get('cart_items[%s][AMT]' % i),
                    'QTY':    request.POST.get('cart_items[%s][QTY]' % i)
                }
                cart_items.append(item)
	
        # call the PayPal driver (2)
        driver = PayPal()
        # call the relevant API method (3)
        result = driver.SetExpressCheckout(amount, currency, return_url, cancel_url, cart_items)
        # perform the response (4)
        if not result:
            print driver.apierror
            # show the error message (comes from PayPal API) to the user and redirect him/her to the error page
            if request.user.is_authenticated():
                request.user.message_set.create(message = _(driver.setexpresscheckouterror))
            return HttpResponseRedirect(error_url)
        
        # send him/her to the PayPal website to check his/her order details out
        redirect_url = driver.paypal_url()
        return HttpResponseRedirect(redirect_url)
    
    return render_to_response(template,
                              {'currency': currency,
                               'return_url': return_url,
                               'cancel_url': cancel_url,
                               'error_url' : error_url,
                               }, context_instance = RequestContext(request))