Esempio n. 1
0
 def get(self, request, *args, **kwargs):
     paypal = json.loads(PaymentMean.objects.get(slug='paypal').credentials)
     paypal_token = request.GET['token']
     ec_data = {
         "USER": paypal['username'],
         "PWD": paypal['password'],
         "SIGNATURE": paypal['signature'],
         "METHOD": "GetExpressCheckoutDetails",
         "VERSION": 124.0,
         "TOKEN": paypal_token
     }
     try:
         response = requests.post(EC_ENDPOINT, data=ec_data)
         result = parse_paypal_response(response.content.decode('utf-8'))
         ACK = result['ACK']
         if ACK == 'Success' or ACK == 'SuccessWithWarning':
             request.session['token'] = paypal_token
             request.session['payer_id'] = request.GET['PayerID']
             context = self.get_context_data(**kwargs)
             context['amount'] = urlunquote(result['PAYMENTREQUEST_0_AMT'])
             return render(request, self.template_name, context)
         else:
             context = self.get_context_data(**kwargs)
             if getattr(settings, 'DEBUG', False):
                 context['paypal_error'] = urlunquote(response.content.decode('utf-8'))
             else:
                 context['paypal_error'] = urlunquote(result['L_LONGMESSAGE0'])
             return render(request, 'donation/paypal/cancel.html', context)
     except Exception as e:
         if getattr(settings, 'DEBUG', False):
             raise e
         context = self.get_context_data(**kwargs)
         context['server_error'] = 'Could not proceed transaction due to server error. Contact administrator.'
         return render(request, 'donation/paypal/cancel.html', context)
Esempio n. 2
0
 def get(self, request, *args, **kwargs):
     service = get_service_instance()
     paypal = json.loads(PaymentMean.objects.get(slug='paypal').credentials)
     paypal_token = request.GET['token']
     ec_data = {
         "USER": paypal['username'],
         "PWD": paypal['password'],
         "SIGNATURE": paypal['signature'],
         "METHOD": "GetExpressCheckoutDetails",
         "VERSION": 124.0,
         "TOKEN": paypal_token
     }
     try:
         response = requests.post(EC_ENDPOINT, data=ec_data)
         result = parse_paypal_response(response.content.decode('utf-8'))
         ACK = result['ACK']
         if ACK == 'Success' or ACK == 'SuccessWithWarning':
             request.session['token'] = paypal_token
             request.session['payer_id'] = request.GET['PayerID']
             context = self.get_context_data(**kwargs)
             context['amount'] = urlunquote(result['PAYMENTREQUEST_0_AMT'])
             return render(request, self.template_name, context)
         else:
             try:
                 Order.objects.get(pk=request.GET['order_id']).delete()
             except Order.DoesNotExist:
                 pass
             context = self.get_context_data(**kwargs)
             if getattr(settings, 'DEBUG', False):
                 context['paypal_error'] = urlunquote(
                     response.content.decode('utf-8'))
             else:
                 context['paypal_error'] = urlunquote(
                     result['L_LONGMESSAGE0'])
             messages.error(
                 request,
                 'API Error: Confirming PayPal transaction failed.')
             return HttpResponseRedirect(reverse('mediashop:cart'))
     except Exception as e:
         logger.error("%s - PayPal error." % service.project_name,
                      exc_info=True)
         if getattr(settings, 'DEBUG', False):
             raise e
         messages.error(
             request, 'Server Error: Confirming PayPal transaction failed.')
         return HttpResponseRedirect(reverse('mediashop:cart'))
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        service = get_service_instance()
        config = service.config
        payment_mean = PaymentMean.objects.get(slug='paypal')
        if getattr(settings, 'DEBUG', False):
            paypal = json.loads(payment_mean.credentials)
        else:
            try:
                paypal = json.loads(payment_mean.credentials)
            except:
                return HttpResponse("Error, Could not parse PayPal parameters.")

        amount = request.POST["amount"]

        if getattr(settings, 'UNIT_TESTING', False):
            signature = 'dumb_signature'
        else:
            signature = ''.join([random.SystemRandom().choice(string.ascii_letters + string.digits) for n in range(16)])
        request.session['amount'] = amount
        request.session['signature'] = signature
        request.session['return_url'] = service.url + reverse('donation:home')

        if getattr(settings, 'UNIT_TESTING', False):
            return HttpResponse(json.dumps({"amount": amount}))

        line_items = {
            "L_PAYMENTREQUEST_0_NAME0": 'Donation',
            "L_PAYMENTREQUEST_0_DESC0": '<' + _("No description") + '>',
            "L_PAYMENTREQUEST_0_AMT0": amount,
            "L_PAYMENTREQUEST_0_QTY0": 1,
            "L_PAYMENTREQUEST_0_TAXAMT0": 0,
            "L_PAYMENTREQUEST_0_NUMBER0": 1,
            "L_PAYMENTREQUEST_0_ITEMURL0": service.url + reverse('donation:home'),
            "L_PAYMENTREQUEST_0_ITEMCATEGORY0": 'Physical'
        }

        ec_data = {
            "USER": paypal['username'],
            "PWD": paypal['password'],
            "SIGNATURE": paypal['signature'],
            "METHOD": "SetExpressCheckout",
            "VERSION": 124.0,
            "RETURNURL": service.url + reverse('donation:paypal_get_details') + '?amount=' + amount,
            "CANCELURL": service.url + reverse('donation:paypal_cancel'),
            "PAYMENTREQUEST_0_PAYMENTACTION": "Sale",
            "PAYMENTREQUEST_0_AMT": amount,
            "PAYMENTREQUEST_0_ITEMAMT": amount,
            "PAYMENTREQUEST_0_SHIPPINGAMT": 0,
            "PAYMENTREQUEST_0_TAXAMT": 0,
            "PAYMENTREQUEST_0_CURRENCYCODE": "EUR",
            "PAYMENTREQUEST_0_DESC": "Donation on " + service.project_name
        }
        ec_data.update(line_items)
        try:
            response = requests.post(EC_ENDPOINT, data=ec_data)
            result = parse_paypal_response(response.content.decode('utf-8'))
            ACK = result['ACK']
            if ACK == 'Success' or ACK == 'SuccessWithWarning':
                if getattr(settings, 'DEBUG', False):
                    redirect_url = 'https://www.sandbox.paypal.com/checkoutnow?token=' + result['TOKEN']
                else:
                    redirect_url = 'https://www.paypal.com/checkoutnow?token=' + result['TOKEN']
                return HttpResponseRedirect(redirect_url)
            else:
                context = self.get_context_data(**kwargs)
                if getattr(settings, 'DEBUG', False):
                    context['paypal_error'] = urlunquote(response.content.decode('utf-8'))
                else:
                    context['paypal_error'] = urlunquote(result['L_LONGMESSAGE0'])
                return render(request, 'donation/institution/home.html', context)
        except Exception as e:
            if getattr(settings, 'DEBUG', False):
                raise e
            context = self.get_context_data(**kwargs)
            context['server_error'] = 'Could not initiate transaction due to server error. Contact administrator.'
            return render(request, 'donation/institution/home.html', context)
Esempio n. 4
0
    def post(self, request, *args, **kwargs):
        if getattr(settings, 'UNIT_TESTING', False):
            return confirm_checkout(request, signature=request.session['signature'], *args, **kwargs)

        service = get_service_instance()
        paypal = json.loads(PaymentMean.objects.get(slug='paypal').credentials)
        amount = request.POST['amount']
        line_items = {
            "L_PAYMENTREQUEST_0_NAME0": "Donation",
            "L_PAYMENTREQUEST_0_DESC0": '<' + _("No description") + '>',
            "L_PAYMENTREQUEST_0_AMT0": amount,
            "L_PAYMENTREQUEST_0_QTY0": 1,
            "L_PAYMENTREQUEST_0_TAXAMT0": 0,
            "L_PAYMENTREQUEST_0_NUMBER0": 1,
            "L_PAYMENTREQUEST_0_ITEMURL0":  service.url + reverse('donation:home'),
            "L_PAYMENTREQUEST_0_ITEMCATEGORY0": 'Physical'
        }
        ec_data = {
            "USER": paypal['username'],
            "PWD": paypal['password'],
            "SIGNATURE": paypal['signature'],
            "METHOD": "DoExpressCheckoutPayment",
            "VERSION": 124.0,
            "TOKEN": request.session['token'],
            "PAYERID": request.session['payer_id'],
            "PAYMENTREQUEST_0_PAYMENTACTION": "Sale",
            "PAYMENTREQUEST_0_AMT": amount,
            "PAYMENTREQUEST_0_ITEMAMT": amount,
            "PAYMENTREQUEST_0_SHIPPINGAMT": 0,
            "PAYMENTREQUEST_0_TAXAMT": 0,
            "PAYMENTREQUEST_0_CURRENCYCODE": "EUR",
            "PAYMENTREQUEST_0_DESC": "Donation on " + service.project_name
        }
        ec_data.update(line_items)
        if getattr(settings, 'DEBUG', False):
            response = requests.post(EC_ENDPOINT, data=ec_data)
            result = parse_paypal_response(response.content.decode('utf-8'))
            ACK = result['ACK']
            if ACK == 'Success' or ACK == 'SuccessWithWarning':
                request.session['mean'] = 'paypal'
                return donation_do_checkout(request, signature=request.session['signature'], *args, **kwargs)
            else:
                context = self.get_context_data(**kwargs)
                if getattr(settings, 'DEBUG', False):
                    context['paypal_error'] = urlunquote(response.content.decode('utf-8'))
                else:
                    context['paypal_error'] = urlunquote(result['L_LONGMESSAGE0'])
                return render(request, 'donation/institution/home.html', context)
        else:
            try:
                response = requests.post(EC_ENDPOINT, data=ec_data)
                result = parse_paypal_response(response.content.decode('utf-8'))
                ACK = result['ACK']
                if ACK == 'Success' or ACK == 'SuccessWithWarning':
                    return confirm_checkout(request, signature=request.session['signature'], *args, **kwargs)
                else:
                    context = self.get_context_data(**kwargs)
                    if getattr(settings, 'DEBUG', False):
                        context['paypal_error'] = urlunquote(response.content.decode('utf-8'))
                    else:
                        context['paypal_error'] = urlunquote(result['L_LONGMESSAGE0'])
                    return render(request,'donation/paypal/cancel.html', context)
            except Exception as e:
                context = self.get_context_data(**kwargs)
                context['server_error'] = 'Could not proceed transaction due to server error. Contact administrator.'
                return render(request, 'donation/paypal/cancel.html', context)
Esempio n. 5
0
    def post(self, request, *args, **kwargs):
        service = get_service_instance()
        payment_mean = PaymentMean.objects.get(slug='paypal')
        if getattr(settings, 'DEBUG', False):
            paypal = json.loads(payment_mean.credentials)
        else:
            try:
                paypal = json.loads(payment_mean.credentials)
            except:
                return HttpResponse(
                    "Error, Could not parse PayPal parameters.")

        try:
            order = parse_order_info(request)
        except:
            messages.error(request, "Failed to parse order information")
            return HttpResponseRedirect(reverse('mediashop:cart'))

        if getattr(settings, 'UNIT_TESTING', False):
            signature = 'dumb_signature'
        else:
            signature = ''.join([
                random.SystemRandom().choice(string.ascii_letters +
                                             string.digits) for n in range(16)
            ])
        request.session['signature'] = signature
        request.session['return_url'] = service.url + reverse(
            'mediashop:download', args=(order.id, ))

        if getattr(settings, 'UNIT_TESTING', False):
            return HttpResponse(json.dumps({"order_id": order.id}))

        line_items = {}
        for i in range(len(order.album_list)):
            album = order.album_list[i]
            line_items.update({
                "L_PAYMENTREQUEST_0_NAME%d" % i:
                album.title,
                "L_PAYMENTREQUEST_0_DESC%d" % i:
                album.title,
                "L_PAYMENTREQUEST_0_AMT%d" % i:
                do_currency(album.cost, order.currency.code),
                "L_PAYMENTREQUEST_0_QTY%d" % i:
                1,
                "L_PAYMENTREQUEST_0_TAXAMT%d" % i:
                0,
                "L_PAYMENTREQUEST_0_NUMBER%d" % i:
                i + 1,
                "L_PAYMENTREQUEST_0_ITEMURL%d" % i:
                service.url + reverse('mediashop:music_item_detail',
                                      args=(
                                          album.artist.slug,
                                          album.slug,
                                      )),
                "L_PAYMENTREQUEST_0_ITEMCATEGORY%d" % i:
                'Digital'
            })

        for i in range(len(order.song_list)):
            song = order.song_list[i]
            line_items.update({
                "L_PAYMENTREQUEST_0_NAME%d" % i:
                song.title,
                "L_PAYMENTREQUEST_0_DESC%d" % i:
                song.title,
                "L_PAYMENTREQUEST_0_AMT%d" % i:
                do_currency(song.cost, order.currency.code),
                "L_PAYMENTREQUEST_0_QTY%d" % i:
                1,
                "L_PAYMENTREQUEST_0_TAXAMT%d" % i:
                0,
                "L_PAYMENTREQUEST_0_NUMBER%d" % i:
                i + 1,
                "L_PAYMENTREQUEST_0_ITEMURL%d" % i:
                service.url + reverse('mediashop:music_item_detail',
                                      args=(
                                          song.artist.slug,
                                          song.slug,
                                      )),
                "L_PAYMENTREQUEST_0_ITEMCATEGORY%d" % i:
                'Digital'
            })

        ec_data = {
            "USER":
            paypal['username'],
            "PWD":
            paypal['password'],
            "SIGNATURE":
            paypal['signature'],
            "METHOD":
            "SetExpressCheckout",
            "VERSION":
            124.0,
            "RETURNURL":
            service.url + reverse('mediashop:paypal_get_details') +
            '?order_id=' + order.id,
            "CANCELURL":
            service.url + reverse('mediashop:cart'),
            "PAYMENTREQUEST_0_PAYMENTACTION":
            "Sale",
            "PAYMENTREQUEST_0_AMT":
            do_currency(order.total_cost, order.currency.code),
            "PAYMENTREQUEST_0_ITEMAMT":
            do_currency(order.total_cost, order.currency.code),
            "PAYMENTREQUEST_0_SHIPPINGAMT":
            0,
            "PAYMENTREQUEST_0_TAXAMT":
            0,
            "PAYMENTREQUEST_0_CURRENCYCODE":
            order.currency.code,
            "PAYMENTREQUEST_0_DESC":
            "Purchase on " + service.project_name
        }
        ec_data.update(line_items)
        try:
            response = requests.post(EC_ENDPOINT, data=ec_data)
            result = parse_paypal_response(response.content.decode('utf-8'))
            ACK = result['ACK']
            if ACK == 'Success' or ACK == 'SuccessWithWarning':
                if getattr(settings, 'DEBUG', False):
                    redirect_url = 'https://www.sandbox.paypal.com/checkoutnow?token=' + result[
                        'TOKEN']
                else:
                    redirect_url = 'https://www.paypal.com/checkoutnow?token=' + result[
                        'TOKEN']
                return HttpResponseRedirect(redirect_url)
            else:
                order.delete()
                context = self.get_context_data(**kwargs)
                if getattr(settings, 'DEBUG', False):
                    context['paypal_error'] = urlunquote(
                        response.content.decode('utf-8'))
                else:
                    context['paypal_error'] = urlunquote(
                        result['L_LONGMESSAGE0'])
                messages.error(request,
                               'Initiating PayPal transaction failed.')
                return HttpResponseRedirect(reverse('mediashop:cart'))
        except Exception as e:
            logger.error("%s - PayPal error." % service.project_name,
                         exc_info=True)
            if getattr(settings, 'DEBUG', False):
                raise e
            context = self.get_context_data(**kwargs)
            messages.error(
                request,
                'Could not initiate transaction due to server error. Contact administrator.'
            )
            return render(request, 'shopping/paypal/cancel.html', context)
Esempio n. 6
0
    def post(self, request, *args, **kwargs):
        if getattr(settings, 'UNIT_TESTING', False):
            return confirm_checkout(request,
                                    signature=request.session['signature'],
                                    *args,
                                    **kwargs)

        service = get_service_instance()
        paypal = json.loads(PaymentMean.objects.get(slug='paypal').credentials)
        order_id = request.POST['order_id']
        order = Order.objects.get(pk=order_id)
        line_items = {}
        for i in range(len(order.album_list)):
            album = order.album_list[i]
            line_items.update({
                "L_PAYMENTREQUEST_0_NAME%d" % i:
                album.title,
                "L_PAYMENTREQUEST_0_DESC%d" % i:
                album.title,
                "L_PAYMENTREQUEST_0_AMT%d" % i:
                do_currency(album.cost, order.currency.code),
                "L_PAYMENTREQUEST_0_QTY%d" % i:
                1,
                "L_PAYMENTREQUEST_0_TAXAMT%d" % i:
                0,
                "L_PAYMENTREQUEST_0_NUMBER%d" % i:
                i + 1,
                "L_PAYMENTREQUEST_0_ITEMURL%d" % i:
                service.url + reverse('mediashop:music_item_detail',
                                      args=(
                                          album.artist.slug,
                                          album.slug,
                                      )),
                "L_PAYMENTREQUEST_0_ITEMCATEGORY%d" % i:
                'Digital'
            })

        for i in range(len(order.song_list)):
            song = order.song_list[i]
            line_items.update({
                "L_PAYMENTREQUEST_0_NAME%d" % i:
                song.title,
                "L_PAYMENTREQUEST_0_DESC%d" % i:
                song.title,
                "L_PAYMENTREQUEST_0_AMT%d" % i:
                do_currency(song.cost, order.currency.code),
                "L_PAYMENTREQUEST_0_QTY%d" % i:
                1,
                "L_PAYMENTREQUEST_0_TAXAMT%d" % i:
                0,
                "L_PAYMENTREQUEST_0_NUMBER%d" % i:
                i + 1,
                "L_PAYMENTREQUEST_0_ITEMURL%d" % i:
                service.url + reverse('mediashop:music_item_detail',
                                      args=(
                                          song.artist.slug,
                                          song.slug,
                                      )),
                "L_PAYMENTREQUEST_0_ITEMCATEGORY%d" % i:
                'Digital'
            })

        ec_data = {
            "USER":
            paypal['username'],
            "PWD":
            paypal['password'],
            "SIGNATURE":
            paypal['signature'],
            "METHOD":
            "DoExpressCheckoutPayment",
            "VERSION":
            124.0,
            "TOKEN":
            request.session['token'],
            "PAYERID":
            request.session['payer_id'],
            "PAYMENTREQUEST_0_PAYMENTACTION":
            "Sale",
            "PAYMENTREQUEST_0_AMT":
            do_currency(order.total_cost, order.currency.code),
            "PAYMENTREQUEST_0_ITEMAMT":
            do_currency(order.total_cost, order.currency.code),
            "PAYMENTREQUEST_0_SHIPPINGAMT":
            0,
            "PAYMENTREQUEST_0_TAXAMT":
            0,
            "PAYMENTREQUEST_0_CURRENCYCODE":
            order.currency.code,
            "PAYMENTREQUEST_0_DESC":
            "Purchase on " + service.project_name
        }
        ec_data.update(line_items)
        if getattr(settings, 'DEBUG', False):
            response = requests.post(EC_ENDPOINT, data=ec_data)
            result = parse_paypal_response(response.content.decode('utf-8'))
            ACK = result['ACK']
            if ACK == 'Success' or ACK == 'SuccessWithWarning':
                return confirm_checkout(request,
                                        signature=request.session['signature'],
                                        *args,
                                        **kwargs)
            else:
                try:
                    Order.objects.get(pk=request.POST['order_id']).delete()
                except Order.DoesNotExist:
                    pass
                context = self.get_context_data(**kwargs)
                if getattr(settings, 'DEBUG', False):
                    context['paypal_error'] = urlunquote(
                        response.content.decode('utf-8'))
                else:
                    context['paypal_error'] = urlunquote(
                        result['L_LONGMESSAGE0'])
                messages.error(
                    request,
                    'API Error: Terminating PayPal transaction failed.')
                return HttpResponseRedirect(reverse('mediashop:cart'))
        else:
            try:
                response = requests.post(EC_ENDPOINT, data=ec_data)
                result = parse_paypal_response(
                    response.content.decode('utf-8'))
                ACK = result['ACK']
                if ACK == 'Success' or ACK == 'SuccessWithWarning':
                    return confirm_checkout(
                        request,
                        signature=request.session['signature'],
                        *args,
                        **kwargs)
                else:
                    try:
                        Order.objects.get(pk=request.POST['order_id']).delete()
                    except Order.DoesNotExist:
                        pass
                    context = self.get_context_data(**kwargs)
                    if getattr(settings, 'DEBUG', False):
                        context['paypal_error'] = urlunquote(
                            response.content.decode('utf-8'))
                    else:
                        context['paypal_error'] = urlunquote(
                            result['L_LONGMESSAGE0'])
                    messages.error(
                        request,
                        'API Error: Terminating PayPal transaction failed.')
                    return HttpResponseRedirect(reverse('mediashop:cart'))
            except Exception as e:
                logger.error("%s - PayPal error. Order ID: %s" %
                             (service.project_name, order.id),
                             exc_info=True)
                messages.error(
                    request,
                    'Could not terminate transaction due to server error. Contact administrator.'
                )
                return HttpResponseRedirect(reverse('mediashop:cart'))
Esempio n. 7
0
    def post(self, request, *args, **kwargs):
        service = get_service_instance()
        config = service.config
        payment_mean = PaymentMean.objects.get(slug='paypal')
        if getattr(settings, 'DEBUG', False):
            paypal = json.loads(payment_mean.credentials)
        else:
            try:
                paypal = json.loads(payment_mean.credentials)
            except:
                return HttpResponse(
                    "Error, Could not parse PayPal parameters.")

        try:
            order = parse_order_info(request)
        except:
            return HttpResponseRedirect(reverse('shopping:checkout'))
        order.retailer = service
        order.payment_mean = payment_mean
        order.save()  # Save first to generate the Order id
        order = Order.objects.get(
            pk=order.id
        )  # Grab the newly created object to avoid create another one in subsequent save()
        member = request.user
        if member.is_authenticated():
            order.member = member
        else:
            order.aotc = generate_tx_code(order.id,
                                          order.anonymous_buyer.auto_inc)

        order.rcc = generate_tx_code(order.id, config.rel_id)
        order.save()

        if getattr(settings, 'UNIT_TESTING', False):
            signature = 'dumb_signature'
        else:
            signature = ''.join([
                random.SystemRandom().choice(string.ascii_letters +
                                             string.digits) for n in range(16)
            ])
        request.session['signature'] = signature
        request.session['return_url'] = service.url + reverse(
            'shopping:cart', args=(order.id, ))

        if getattr(settings, 'UNIT_TESTING', False):
            return HttpResponse(json.dumps({"order_id": order.id}))

        line_items = {}
        for i in range(len(order.entries)):
            entry = order.entries[i]
            product = entry.product
            line_items.update({
                "L_PAYMENTREQUEST_0_NAME%d" % i:
                product.name,
                "L_PAYMENTREQUEST_0_DESC%d" % i:
                product.summary if product.summary else '<' +
                _("No description") + '>',
                "L_PAYMENTREQUEST_0_AMT%d" % i:
                do_currency(product.retail_price, order.currency.code),
                "L_PAYMENTREQUEST_0_QTY%d" % i:
                entry.count,
                "L_PAYMENTREQUEST_0_TAXAMT%d" % i:
                0,
                "L_PAYMENTREQUEST_0_NUMBER%d" % i:
                i + 1,
                "L_PAYMENTREQUEST_0_ITEMURL%d" % i:
                service.url + reverse('shopping:product_detail',
                                      args=(
                                          product.category.slug,
                                          product.slug,
                                      )),
                "L_PAYMENTREQUEST_0_ITEMCATEGORY%d" % i:
                'Physical'
            })

        ec_data = {
            "USER":
            paypal['username'],
            "PWD":
            paypal['password'],
            "SIGNATURE":
            paypal['signature'],
            "METHOD":
            "SetExpressCheckout",
            "VERSION":
            124.0,
            "RETURNURL":
            service.url + reverse('shopping:paypal_get_details') +
            '?order_id=' + order.id,
            "CANCELURL":
            service.url + reverse('shopping:paypal_cancel'),
            "PAYMENTREQUEST_0_PAYMENTACTION":
            "Sale",
            "PAYMENTREQUEST_0_AMT":
            do_currency(order.total_cost, order.currency.code),
            "PAYMENTREQUEST_0_ITEMAMT":
            do_currency(order.items_cost, order.currency.code),
            "PAYMENTREQUEST_0_SHIPPINGAMT":
            do_currency(order.delivery_option.cost, order.currency.code),
            "PAYMENTREQUEST_0_TAXAMT":
            0,
            "PAYMENTREQUEST_0_CURRENCYCODE":
            order.currency.code,
            "PAYMENTREQUEST_0_DESC":
            "Purchase on " + service.project_name
        }
        ec_data.update(line_items)
        try:
            response = requests.post(EC_ENDPOINT, data=ec_data)
            result = parse_paypal_response(response.content.decode('utf-8'))
            ACK = result['ACK']
            if ACK == 'Success' or ACK == 'SuccessWithWarning':
                if getattr(settings, 'DEBUG', False):
                    redirect_url = 'https://www.sandbox.paypal.com/checkoutnow?token=' + result[
                        'TOKEN']
                else:
                    redirect_url = 'https://www.paypal.com/checkoutnow?token=' + result[
                        'TOKEN']
                return HttpResponseRedirect(redirect_url)
            else:
                order.delete()
                context = self.get_context_data(**kwargs)
                if getattr(settings, 'DEBUG', False):
                    context['paypal_error'] = urlunquote(
                        response.content.decode('utf-8'))
                else:
                    context['paypal_error'] = urlunquote(
                        result['L_LONGMESSAGE0'])
                return render(request, 'shopping/paypal/cancel.html', context)
        except Exception as e:
            logger.error("%s - PayPal error." % service.project_name,
                         exc_info=True)
            if getattr(settings, 'DEBUG', False):
                raise e
            context = self.get_context_data(**kwargs)
            context[
                'server_error'] = 'Could not initiate transaction due to server error. Contact administrator.'
            return render(request, 'shopping/paypal/cancel.html', context)
Esempio n. 8
0
    def post(self, request, *args, **kwargs):
        if getattr(settings, 'UNIT_TESTING', False):
            return confirm_checkout(request,
                                    signature=request.session['signature'],
                                    *args,
                                    **kwargs)

        service = get_service_instance()
        paypal = json.loads(PaymentMean.objects.get(slug='paypal').credentials)
        order_id = request.POST['order_id']
        order = Order.objects.get(pk=order_id)
        line_items = {}
        for i in range(len(order.entries)):
            entry = order.entries[i]
            product = entry.product
            line_items.update({
                "L_PAYMENTREQUEST_0_NAME%d" % i:
                product.name,
                "L_PAYMENTREQUEST_0_DESC%d" % i:
                product.summary if product.summary else '<' +
                _("No description") + '>',
                "L_PAYMENTREQUEST_0_AMT%d" % i:
                do_currency(product.retail_price, order.currency.code),
                "L_PAYMENTREQUEST_0_QTY%d" % i:
                entry.count,
                "L_PAYMENTREQUEST_0_TAXAMT%d" % i:
                0,
                "L_PAYMENTREQUEST_0_NUMBER%d" % i:
                i + 1,
                "L_PAYMENTREQUEST_0_ITEMURL%d" % i:
                service.url + reverse('shopping:product_detail',
                                      args=(
                                          product.category.slug,
                                          product.slug,
                                      )),
                "L_PAYMENTREQUEST_0_ITEMCATEGORY%d" % i:
                'Physical'
            })
        ec_data = {
            "USER":
            paypal['username'],
            "PWD":
            paypal['password'],
            "SIGNATURE":
            paypal['signature'],
            "METHOD":
            "DoExpressCheckoutPayment",
            "VERSION":
            124.0,
            "TOKEN":
            request.session['token'],
            "PAYERID":
            request.session['payer_id'],
            "PAYMENTREQUEST_0_PAYMENTACTION":
            "Sale",
            "PAYMENTREQUEST_0_AMT":
            do_currency(order.total_cost, order.currency.code),
            "PAYMENTREQUEST_0_ITEMAMT":
            do_currency(order.items_cost, order.currency.code),
            "PAYMENTREQUEST_0_SHIPPINGAMT":
            do_currency(order.delivery_option.cost, order.currency.code),
            "PAYMENTREQUEST_0_TAXAMT":
            0,
            "PAYMENTREQUEST_0_CURRENCYCODE":
            order.currency.code,
            "PAYMENTREQUEST_0_DESC":
            "Purchase on " + service.project_name
        }
        ec_data.update(line_items)
        if getattr(settings, 'DEBUG', False):
            response = requests.post(EC_ENDPOINT, data=ec_data)
            result = parse_paypal_response(response.content.decode('utf-8'))
            ACK = result['ACK']
            if ACK == 'Success' or ACK == 'SuccessWithWarning':
                return confirm_checkout(request,
                                        signature=request.session['signature'],
                                        *args,
                                        **kwargs)
            else:
                try:
                    Order.objects.get(pk=request.POST['order_id']).delete()
                except Order.DoesNotExist:
                    pass
                context = self.get_context_data(**kwargs)
                if getattr(settings, 'DEBUG', False):
                    context['paypal_error'] = urlunquote(
                        response.content.decode('utf-8'))
                else:
                    context['paypal_error'] = urlunquote(
                        result['L_LONGMESSAGE0'])
                return render(request, 'shopping/paypal/cancel.html', context)
        else:
            try:
                response = requests.post(EC_ENDPOINT, data=ec_data)
                result = parse_paypal_response(
                    response.content.decode('utf-8'))
                ACK = result['ACK']
                if ACK == 'Success' or ACK == 'SuccessWithWarning':
                    return confirm_checkout(
                        request,
                        signature=request.session['signature'],
                        *args,
                        **kwargs)
                else:
                    try:
                        Order.objects.get(pk=request.POST['order_id']).delete()
                    except Order.DoesNotExist:
                        pass
                    context = self.get_context_data(**kwargs)
                    if getattr(settings, 'DEBUG', False):
                        context['paypal_error'] = urlunquote(
                            response.content.decode('utf-8'))
                    else:
                        context['paypal_error'] = urlunquote(
                            result['L_LONGMESSAGE0'])
                    return render(request, 'shopping/paypal/cancel.html',
                                  context)
            except Exception as e:
                logger.error("%s - PayPal error. Order ID: %s" %
                             (service.project_name, order.id),
                             exc_info=True)
                context = self.get_context_data(**kwargs)
                context[
                    'server_error'] = 'Could not proceed transaction due to server error. Contact administrator.'
                return render(request, 'shopping/paypal/cancel.html', context)