Esempio n. 1
0
def docheckout(request,
               error_url,
               success_url,
               template="paypal/docheckout.html",
               currency="USD"):
    """
    Django view to do the actual payment (charges actual money)
    It performs the relevant API method DoExpressCheckoutPayment
    """

    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)

        # perform GET
        token = request.GET.get("token")
        payerid = request.GET.get("PayerID")

        # charge from PayPal
        result, response = process_payment_request(amount, currency, token,
                                                   payerid)
        # process the result
        if not result:
            # show the error message (comes from PayPal API) and redirect user to the error page
            if request.user.is_authenticated():
                request.user.message_set.create(message=_(
                    "Amount %(amount)s has not been charged, server error is '%(error)s'"
                    % {
                        'amount': amount,
                        'error': response.error
                    }))
            return HttpResponseRedirect(error_url)

        # Now we are gone, redirect user to success page
        if request.user.is_authenticated():
            request.user.message_set.create(message=_(
                "Amount %(amount)s has been successfully charged, your transaction id is '%(trans_id)s'"
                % {
                    'amount': amount,
                    'trans_id': response.trans_id
                }))

        return HttpResponseRedirect(success_url)

    return render_to_response(template, {
        'error_url': error_url,
        'success_url': success_url,
    },
                              context_instance=RequestContext(request))
Esempio n. 2
0
def do_checkout(request, amount, error_url, success_url, template="paypal/docheckout.html", currency="USD"):
    """
    Django view to do the actual payment (charges actual money)
    It performs the relevant API method DoExpressCheckoutPayment
    """

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

        # perform GET
        token = base64.b64decode(request.GET.get("token"))
        payerid = base64.b64decode(request.GET.get("PayerID"))

        # charge from PayPal
        result, response = process_payment_request(amount, currency, token, payerid)
        # process the result
        if not result:
            # show the error message (comes from PayPal API) and redirect user to the error page
            if request.user.is_authenticated():
                messages.error(request, _("Amount %s has not been charged, "
                                          "server error is '%s'" % (amount, response.error)))
            error_message = "Amount %s has not been charged, server error is '%s'" % (amount, response.error)
            error_message = base64.b64encode(error_message)
            return HttpResponseRedirect(error_url+"?error="+error_message)

        # Now we are gone, redirect user to success page
        if request.user.is_authenticated():
            messages.info(request, _("Amount %s has been successfully charged, "
                                     "your transaction id is '%s'" % (amount, response.trans_id)))
        return HttpResponseRedirect(success_url)

    return render_to_response(template,
                              {'error_url': error_url,
                               'success_url': success_url,
                               }, context_instance=RequestContext(request))
def docheckout(request, error_url, success_url, template = "paypal/docheckout.html", currency = "USD"):
   
    
    if request.POST:
        # normalize the given amount
        amount = itemprice
        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)

        # perform GET
        token   = request.GET.get("token")

        payerid = request.GET.get("PayerID")

        # charge from PayPal
        result, response = process_payment_request(amount, currency, token, payerid)
        # process the result
        if not result:
            # show the error message (comes from PayPal API) and redirect user to the error page
            if request.user.is_authenticated():
                request.user.message_set.create(message = _("Amount %s has been successfully charged, your transaction id is '%s'" % (amount, response.error)))
            return HttpResponseRedirect(error_url)

        # Now we are gone, redirect user to success page
        if request.user.is_authenticated():
            request.user.message_set.create(message = _("Amount %s has been successfully charged, your transaction id is '%s'" % (amount, response.trans_id)))
        
        return HttpResponseRedirect(success_url)

    return render_to_response(template,
                              {'price': itemprice,
                              'error_url': error_url,
                               'success_url': success_url,
                               }, context_instance = RequestContext(request))
Esempio n. 4
0
def payPalDoCheckOut(request, *args, **kwargs):
    """The PayPal method to charge the money, after all verification passed."""
    initParam = {}
    id = request.GET.get("id")
    token = request.GET.get("token")
    payerID = request.GET.get("PayerID")
    initParam['id'] = id
    initParam['token'] = token
    if token and payerID and id:
        #Check and get Service detail information
        checkMethod = kwargs.pop('checkMethod', None)
        if checkMethod:
            gateway = request.session.get('gateway', None)
            if gateway:
                del request.session['gateway']
                initParam['gateway'] = gateway
                serviceDetail = checkMethod(request, initParam=initParam)
                if serviceDetail:
                    amount = serviceDetail.actual_amount
                    currency = serviceDetail.app.currency.currency
                    result, response = utils.process_payment_request(
                        amount, currency, token, payerID)
                    if result:
                        #Do something after payment success.
                        executeMethod = kwargs.pop('executeMethod', None)
                        if executeMethod:
                            initParam['serviceDetail_id'] = serviceDetail.id
                            if executeMethod(request, initParam=initParam):
                                success_page = request.session.get(
                                    'success_page', None)
                                back_page = request.session.get(
                                    'back_page', None)
                                if back_page:
                                    del request.session['back_page']
                                if success_page:
                                    del request.session['success_page']
                                    initParam['success_page'] = success_page
                                    initParam[
                                        'success_page_msg'] = request.session[
                                            'success_page_msg']
                                #For the value in paypal_success.html
                                initParam['app'] = serviceDetail.app
                                initParam['type'] = 'Payment'
                                initParam[
                                    'price'] = serviceDetail.actual_amount
                                initParam['msg'] = _(
                                    'Thank you for your payment, and your app will be listed according to the effective period your choosed.'
                                )
                                log.info(
                                    _('Seller %(param1)s has paid service fee with service detail id %(param2)s.'
                                      ) % {
                                          'param1': request.user.username,
                                          'param2': serviceDetail.id
                                      })
                                return render_to_response(
                                    "payment/paypal_success.html",
                                    initParam,
                                    context_instance=RequestContext(request))
                            else:
                                log.error(
                                    _('Token %(param1)s, PayerID: %(param2)s, Execute method %(param3)s failed.'
                                      ) % {
                                          'param1': token,
                                          'param2': payerID,
                                          'param3': executeMethod.__name__
                                      })
                        else:
                            log.error(
                                _('Token %(param1)s, PayerID: %(param2)s, ExecuteMethod does not exist.'
                                  ) % {
                                      'param1': token,
                                      'param2': payerID
                                  })
                    else:
                        log.error(
                            _('Token %(param1)s, PayerID: %(param2)s, %(param3)s : %(param4)s.'
                              ) % {
                                  'param1': token,
                                  'param2': payerID,
                                  'param3': response.error,
                                  'param4': response.error_msg
                              })
                else:
                    log.error(
                        _('Token %(param1)s, PayerID: %(param2)s, User: %(param3)s, Execute method %(param4)s failed.'
                          ) % {
                              'param1': token,
                              'param2': payerID,
                              'param3': request.user.username,
                              'param4': checkMethod.__name__
                          })
            else:
                log.error(
                    _('Token %(param1)s, PayerID: %(param2)s, Gateway no exists in request.session.'
                      ) % {
                          'param1': token,
                          'param2': payerID
                      })
        else:
            log.error(
                _('Token %(param1)s, PayerID: %(param2)s, CheckMethod does not exist.'
                  ) % {
                      'param1': token,
                      'param2': payerID
                  })
    else:
        log.error(_('Token or PayerID no exists.'))

    if request.session.get('gateway', None):
        del request.session['gateway']
    success_page = request.session.get('success_page', None)
    back_page = request.session.get('back_page', None)
    if success_page:
        del request.session['success_page']
    if back_page:
        del request.session['back_page']
        error_msg = driver.GENERIC_PAYPAL_ERROR
        page_msg = request.session['back_page_msg']
        return render_to_response('payment/paypal_cancel.html', {
            'error_msg': error_msg,
            'back_page': back_page,
            'back_page_msg': page_msg
        },
                                  context_instance=RequestContext(request))
    else:
        error_msg = _('%(param1)s Please payment again.') % {
            'param1': driver.GENERIC_PAYPAL_ERROR
        }
        return render_to_response('payment/paypal_error.html',
                                  {"error_msg": error_msg},
                                  context_instance=RequestContext(request))
Esempio n. 5
0
def payPalDoCheckOut(request, *args, **kwargs):
    """The PayPal method to charge the money, after all verification passed."""
    initParam = {}
    id = request.GET.get("id")
    token = request.GET.get("token")
    payerID = request.GET.get("PayerID")
    initParam['id'] = id
    initParam['token'] = token
    if token and payerID and id:
        #Check and get Service detail information
        checkMethod = kwargs.pop('checkMethod', None)
        if checkMethod:
            gateway = request.session.get('gateway', None)
            if gateway:
                del request.session['gateway']
                initParam['gateway'] = gateway
                serviceDetail = checkMethod(request, initParam=initParam)
                if serviceDetail:
                    amount = serviceDetail.actual_amount
                    currency = serviceDetail.app.currency.currency
                    result, response = utils.process_payment_request(amount, currency, token, payerID)
                    if result:
                        #Do something after payment success.
                        executeMethod = kwargs.pop('executeMethod', None)
                        if executeMethod:
                            initParam['serviceDetail_id'] = serviceDetail.id
                            if executeMethod(request, initParam=initParam):
                                success_page = request.session.get('success_page', None)
                                back_page = request.session.get('back_page', None)
                                if back_page:
                                    del request.session['back_page']
                                if success_page:
                                    del request.session['success_page']
                                    initParam['success_page'] = success_page
                                    initParam['success_page_msg'] = request.session['success_page_msg']
                                #For the value in paypal_success.html
                                initParam['app'] = serviceDetail.app
                                initParam['type'] = 'Payment'
                                initParam['price'] = serviceDetail.actual_amount
                                initParam['msg'] = _('Thank you for your payment, and your app will be listed according to the effective period your choosed.')
                                log.info(_('Seller %(param1)s has paid service fee with service detail id %(param2)s.')
                                          % {'param1': request.user.username, 'param2': serviceDetail.id})
                                return render_to_response("payment/paypal_success.html", initParam, context_instance=RequestContext(request))
                            else:
                                log.error(_('Token %(param1)s, PayerID: %(param2)s, Execute method %(param3)s failed.')
                                          % {'param1': token, 'param2': payerID, 'param3': executeMethod.__name__})
                        else:
                            log.error(_('Token %(param1)s, PayerID: %(param2)s, ExecuteMethod does not exist.')
                                      % {'param1': token, 'param2': payerID})
                    else:
                        log.error(_('Token %(param1)s, PayerID: %(param2)s, %(param3)s : %(param4)s.')
                                  % {'param1': token, 'param2': payerID, 'param3': response.error, 'param4': response.error_msg})
                else:
                    log.error(_('Token %(param1)s, PayerID: %(param2)s, User: %(param3)s, Execute method %(param4)s failed.')
                              % {'param1': token, 'param2': payerID, 'param3': request.user.username, 'param4': checkMethod.__name__})
            else:
                log.error(_('Token %(param1)s, PayerID: %(param2)s, Gateway no exists in request.session.')
                          % {'param1': token, 'param2': payerID})
        else:
            log.error(_('Token %(param1)s, PayerID: %(param2)s, CheckMethod does not exist.')
                      % {'param1': token, 'param2': payerID})
    else:
        log.error(_('Token or PayerID no exists.'))

    if request.session.get('gateway', None):
        del request.session['gateway']
    success_page = request.session.get('success_page', None)
    back_page = request.session.get('back_page', None)
    if success_page:
        del request.session['success_page']
    if back_page:
        del request.session['back_page']
        error_msg = driver.GENERIC_PAYPAL_ERROR
        page_msg = request.session['back_page_msg']
        return render_to_response('payment/paypal_cancel.html',
          {'error_msg': error_msg, 'back_page': back_page, 'back_page_msg': page_msg}, context_instance=RequestContext(request))
    else:
        error_msg = _('%(param1)s Please payment again.') % {'param1': driver.GENERIC_PAYPAL_ERROR}
        return render_to_response('payment/paypal_error.html',
                                  {"error_msg": error_msg}, context_instance=RequestContext(request))