예제 #1
0
파일: views.py 프로젝트: oraclebill/dsusa
def paypal_success_callback(sender, **kwargs):
    logger.debug('paypal_success_callback: sender=%s, kwargs=%s' % (sender, kwargs))
    invnum = sender['invnum']    
    invoice = Invoice.objects.get(pk=invnum)
    invoice.status = Invoice.Const.PAID
    invoice.save()
    register_purchase(invoice.id, invoice.customer, invoice.total, invoice.total_credit)
예제 #2
0
파일: views.py 프로젝트: oraclebill/dsusa
def paypal_checkout(request,  
                    collection_template='product/paypal_checkout.html', 
                    confirmation_template='product/paypal_checkout.html', 
                    success_url=None,
                    form_class=PaymentForm, 
                    extra_context=None):    
    """
    Checkout the current shopping cart via paypal..
    
    Template context:
        - phase
        - account
        - form        
        - cart_items
    
    Checkout consists of three steps:
        1) collect credit card info (while reviewing order info)
           - on submit we do local validation. cycle until this process succeeds or user quits.
           - user can select paypal checkout from the point which initiates alternate flow (managed by the paypal plugin)
        2) display card info and order info for final review
           - on submit we 
               - create an invoice from the currnent cart
               - destroy the current cart
               - send card / order info to payment gateway for processing
           - if processing succeeds
               - find the invoice and mark it paid
           - else if it fails
               - find the invoice and mark it 'failed'. attach failure info to invoice for tracking        
        3) display invoice and success/failure message         
    
    If the user elects paypal checkout, there is an alternate flow.
    """
    
    def item_from_invoice(inv):
        item = {
            "amt":          inv.total,
            "invnum":       inv.id,
            "custom":       request.session.session_key,    # for debugging
            "desc":         inv.description,
            "cancelurl":    make_site_url(reverse('select_products')),     # Express checkout cancel url
            "returnurl":    make_site_url(reverse('dealer-dashboard'))     # Express checkout return url
        } 
        return item

    phase = request.GET.get('p', 'collect')
    logger.debug('entered paypal_checkout: phase=%s, GET=%s, POST=%s', phase, request.GET, request.POST) 
        
    # gather some info we always need
    account = request.user.get_profile().account
    cart = shcart.get_cart_from_request(request)
    cart_items = shcart.get_cart_items(cart)
    template = collection_template
    
    if request.method == 'GET':
        # dieplay cc form
        form = form_class()
    elif request.method == 'POST':
        # validate the cc form. 
        form = form_class(request.POST, request.FILES) 
        if phase == 'collect':
            logger.debug(' paypal_checkout: collect phase...') 
            if form.is_valid():
                template = confirmation_template
                phase = 'confirm'
        elif phase == 'confirm':
            logger.debug(' paypal_checkout: confirm phase...') 
            if form.is_valid():
                logger.debug(' paypal_checkout: confirm phase valid...') 
                invoice = shcart.create_invoice_from_cart(cart, account, request.user)
                response = form._process(request, item_from_invoice(invoice))
                logger.debug(' paypal_checkout: payment response: %s', response) 
                if not response.flag:      
                    invoice.status = Invoice.Const.PAID
                    invoice.save()
                    register_purchase(invoice.id, invoice.customer, invoice.total, invoice.total_credit)          
                    request.user.message_set.create(message='Payment processed successfully - thanks!')
                    if success_url:
                        return HttpResponseRedirect(success_url)
                else:
                    invoice.status = Invoice.Const.CANCELLED
                    #invoice.notes = '%s: %s' % (response.flag_code, response.flag_info)
                    notes = '%s: %s' % (response.flag_code, response.flag_info)
                    invoice.save()
                    request.user.message_set.create(message='Payment processing error - %s' % notes)
                return HttpResponseRedirect(reverse('invoice-detail', kwargs={'object_id': invoice.id}))
            else:
                phase = 'collect'  # back up...
        else:
            return HttpResponseServerError("Internal error - illegal program state: %s" % phase)            
                
    context = locals()
    context.pop('request')
    logger.debug(' paypal_checkout: exiting with context: %s', context) 
    return render_to_response( template, context, context_instance=RequestContext(request))