Ejemplo n.º 1
0
def paynow(request):
    from payments.models import  PayPalShopSettings, PayPalToken
    from preferences.models import Preference
        
    shop = request.shop
    cart = request.cart

    #### Verify Products Availability
    if not cart.is_available():
        request.flash['message'] = 'Items not longer available: '
        for item in cart.items_not_availables():
            request.flash['message'] += item.product.title
        cart.remove_not_available_items()
        
        return HttpResponseRedirect(reverse('my_shopping'))

    
    try:   
        paypal_settings = PayPalShopSettings.objects.filter(shop = shop).get()
    except PayPalShopSettings.DoesNotExist:
        request.flash['message'] = unicode(_("This shop haven't Paypal as a payment provider, please try other method."))
        request.flash['severity'] = "error"
        return HttpResponseRedirect(reverse('my_shopping'))
    
    total_amount = "%0.2f" % cart.total_with_taxes()
    
    return_url = request.build_absolute_uri(reverse("paypal_success"))
    cancel_url = request.build_absolute_uri(reverse("paypal_cancel"))
    
    ppgw = PayPalGateway(username=settings.PAYPAL_USERNAME,
                         password=settings.PAYPAL_PASSWORD,
                         sign=settings.PAYPAL_SIGNATURE,
                         debug=settings.PAYPAL_DEBUG)
    

    payment_request = {
        'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale',
        'PAYMENTREQUEST_0_AMT': total_amount,
        #'PAYMENTREQUEST_0_TAXAMT': "%0.2f" % cart.taxes(),
        #'PAYMENTREQUEST_n_SHIPPINGAMT': "%0.2f" % cart.shipping_charge(),
        #'PAYMENTREQUEST_0_ITEMAMT': "%0.2f" % cart.total(),
        'PAYMENTREQUEST_0_CURRENCYCODE': Preference.get_preference(shop).checkout_currency,
        'PAYMENTREQUEST_0_NOTIFYURL': request.build_absolute_uri(reverse("payments_paypal_ipn")),
        'SUBJECT': paypal_settings.email
    }
    
    #for i, cart_item in enumerate(cart.cartitem_set.all()):
    #    payment_request['L_PAYMENTREQUEST_0_NAME%d' % i] = cart_item.product.title.title() 
    #    payment_request['L_PAYMENTREQUEST_0_AMT%d' % i] = "%0.2f" % cart_item.product.price
    #    payment_request['L_PAYMENTREQUEST_0_QTY%d' % i] = cart_item.qty

    success = ppgw.SetExpressCheckout(payment_request, return_url, cancel_url)
    if success:
        """
        token = A timestamped token by which you identify to PayPal that you are processing
        this payment with Express Checkout. The token expires after three hours. 
        If you set the token in the SetExpressCheckout request, the value of the token in the 
        response is identical to the value in the request.
        Character length and limitations: 20 single-byte characters 
        """
        token = ppgw.token
        PayPalToken(cart=cart, token=token).save()
        return HttpResponseRedirect(ppgw.paypal_url())
    
    else:
        logging.critical("SetExpressCheckout failed. RESPONSE = %s" % ppgw.api_response)
        request.flash['message'] = unicode(_("Payment failed, try other method."))
        request.flash['severity'] = "error"
        return HttpResponseRedirect(reverse('my_shopping'))