Ejemplo n.º 1
0
def payment_paypal_return(request, action):
    """
        Process the request of the shop admin coming back from paypal. 
    """
    from payments.gateways.paypal import PayPalGateway
    from payments.models import PayPalShopSettings
    if action == 'agree':
        ppgw = PayPalGateway(username=settings.PAYPAL_USERNAME,
                             password=settings.PAYPAL_PASSWORD,
                             sign=settings.PAYPAL_SIGNATURE,
                             debug=settings.PAYPAL_DEBUG)
        success, response = ppgw.GetAccessPermissionDetails(
            request.GET['token'])

        if not success:
            request.flash['message'] = unicode(
                _("Preferences not saved. PayPal respond: %s" %
                  response["L_SHORTMESSAGE0"][0]))
            request.flash['severity'] = "error"
            return HttpResponseRedirect(reverse('preferences_payment_paypal'))

        perms = []
        for key in response.iterkeys():
            if 'L_ACCESSPERMISSIONSTATUS' not in key:
                continue
            if response[key][0] == 'Accepted':
                idx = key.split('L_ACCESSPERMISSIONSTATUS')[1]
                perms.append(response['L_ACCESSPERMISSIONNAME%s' % idx][0])

        try:
            paypalinst = PayPalShopSettings.objects.get(shop=request.shop)
        except PayPalShopSettings.DoesNotExist:
            paypalinst = PayPalShopSettings(shop=request.shop)

        paypalinst.payer_id = response['PAYERID'][0]
        paypalinst.email = response['EMAIL'][0]
        paypalinst.first_name = response['FIRSTNAME'][0]
        paypalinst.last_name = response['LASTNAME'][0]
        paypalinst.perms = perms
        paypalinst.save()

        request.flash['message'] = unicode(
            _("Preferences successfully saved."))
        request.flash['severity'] = "success"

    if action == 'cancel':
        request.flash['message'] = unicode(
            _("You have decided not to complete the setup process. Your Paypal payment option is still disabled."
              ))
        request.flash['severity'] = "error"

    if action == 'logout':
        request.flash['message'] = unicode(
            _("You are logged out from Paypal!"))
        request.flash['severity'] = "error"

    return HttpResponseRedirect(reverse('preferences_payment_paypal'))