Exemplo n.º 1
0
def pay_online(request,
               payment_id,
               template_name='payments/stripe/payonline.html'):
    with transaction.atomic():
        payment = get_object_or_404(Payment.objects.select_for_update(),
                                    pk=payment_id)
        form = StripeCardForm(request.POST or None)
        billing_info_form = BillingInfoForm(request.POST or None,
                                            instance=payment)
        currency = get_setting('site', 'global', 'currency')
        if not currency:
            currency = 'usd'
        if request.method == "POST":
            if form.is_valid():
                # get stripe token and make a payment immediately
                stripe.api_key = getattr(settings, 'STRIPE_SECRET_KEY', '')
                token = request.POST.get('stripe_token')

                if billing_info_form.is_valid():
                    payment = billing_info_form.save()

                # create the charge on Stripe's servers - this will charge the user's card
                params = {
                    'amount':
                    math.trunc(payment.amount * 100),  # amount in cents, again
                    'currency': currency,
                    'card': token,
                    'description': payment.description
                }

                try:
                    charge_response = stripe.Charge.create(**params)
                    # an example of response: https://api.stripe.com/v1/charges/ch_YjKFjLIItzRDv7
                    #charge_response = simplejson.loads(charge)
                except Exception as e:
                    charge_response = e.message

                # update payment status and object
                if not payment.is_approved:  # if not already processed
                    payment_update_stripe(request, charge_response, payment)
                    payment_processing_object_updates(request, payment)

                    # log an event
                    log_payment(request, payment)

                    # send payment recipients notification
                    send_payment_notice(request, payment)

                # redirect to thankyou
                return HttpResponseRedirect(
                    reverse('stripe.thank_you', args=[payment.id]))

        return render_to_response(template_name, {
            'form': form,
            'billing_info_form': billing_info_form,
            'payment': payment
        },
                                  context_instance=RequestContext(request))
Exemplo n.º 2
0
def pay_online(request, payment_id, template_name='payments/stripe/payonline.html'):
    payment = get_object_or_404(Payment, pk=payment_id)
    form = StripeCardForm(request.POST or None)
    billing_info_form = BillingInfoForm(request.POST or None, instance=payment)
    currency = get_setting('site', 'global', 'currency')
    if not currency:
        currency = 'usd'
    if request.method == "POST":
        if form.is_valid():
            # get stripe token and make a payment immediately
            stripe.api_key = getattr(settings, 'STRIPE_SECRET_KEY', '')
            token = request.POST.get('stripe_token')

            if billing_info_form.is_valid():
                payment = billing_info_form.save()

            # create the charge on Stripe's servers - this will charge the user's card
            params = {
                       'amount': math.trunc(payment.amount * 100), # amount in cents, again
                       'currency': currency,
                       'card': token,
                       'description': payment.description
                      }

            try:
                charge_response = stripe.Charge.create(**params)
                # an example of response: https://api.stripe.com/v1/charges/ch_YjKFjLIItzRDv7
                #charge_response = simplejson.loads(charge)
            except:
                charge_response = traceback.format_exc()
                print 'error=', charge_response


            # update payment status and object
            if  payment.invoice.balance > 0:
                payment_update_stripe(request, charge_response, payment)
                payment_processing_object_updates(request, payment)

                # log an event
                log_payment(request, payment)

                # send payment recipients notification
                send_payment_notice(request, payment)

            # redirect to thankyou
            return HttpResponseRedirect(reverse('stripe.thank_you', args=[payment.id]))

    return render_to_response(template_name, {'form': form,
                                              'billing_info_form': billing_info_form,
                                              'payment': payment},
                              context_instance=RequestContext(request))
Exemplo n.º 3
0
def pay_online(request, payment_id, template_name='payments/stripe/payonline.html'):
    with transaction.atomic():
        payment = get_object_or_404(Payment.objects.select_for_update(), pk=payment_id)
        form = StripeCardForm(request.POST or None)
        billing_info_form = BillingInfoForm(request.POST or None, instance=payment)
        currency = get_setting('site', 'global', 'currency')
        if not currency:
            currency = 'usd'
        if request.method == "POST":
            if form.is_valid():
                # get stripe token and make a payment immediately
                stripe.api_key = getattr(settings, 'STRIPE_SECRET_KEY', '')
                token = request.POST.get('stripe_token')

            if billing_info_form.is_valid():
                payment = billing_info_form.save()
                
            # determine if we need to create a stripe customer (for membership auto renew)
            customer = False
            obj_user = None
            membership = None
            obj = payment.invoice.get_object()
            if obj and hasattr(obj, 'memberships'):
                membership = obj.memberships()[0]
                if membership.auto_renew and not membership.has_rp(platform='stripe'):
                    obj_user = membership.user
                else:
                    membership = None
                    
            if obj_user:
                try:
                    # Create a Customer:
                    customer = stripe.Customer.create(
                                email=obj_user.email,
                                description="For membership auto renew",
                                source=token,
                    )
                except:
                    customer = None

            # create the charge on Stripe's servers - this will charge the user's card
            params = {
                       'amount': math.trunc(payment.amount * 100), # amount in cents, again
                       'currency': currency,
                       'description': payment.description
                      }
            if customer:
                params.update({'customer': customer.id})
            else:
                params.update({'card': token})

            try:
                charge_response = stripe.Charge.create(**params)
                # an example of response: https://api.stripe.com/v1/charges/ch_YjKFjLIItzRDv7
                #charge_response = simplejson.loads(charge)
            except stripe.error.CardError as e:
                # it's a decline
                json_body = e.json_body
                err  = json_body and json_body['error']
                code = err and err['code']
                message = err and err['message']
                charge_response = '{message} status={status}, code={code}'.format(
                            message=message, status=e.http_status, code=code)
            except Exception as e:
                charge_response = e.message
               
            # add a rp entry now 
            if hasattr(charge_response,'paid') and charge_response.paid:
                if customer and membership:
                    kwargs = {'platform': 'stripe',
                              'customer_profile_id': customer.id,
                              }
                    membership.get_or_create_rp(request.user, **kwargs)

            # update payment status and object
            if not payment.is_approved:  # if not already processed
                payment_update_stripe(request, charge_response, payment)
                payment_processing_object_updates(request, payment)

                # log an event
                log_payment(request, payment)

                # send payment recipients notification
                send_payment_notice(request, payment)

            # redirect to thankyou
            return HttpResponseRedirect(reverse('stripe.thank_you', args=[payment.id]))

    return render_to_response(template_name, {'form': form,
                                              'billing_info_form': billing_info_form,
                                              'STRIPE_PUBLISHABLE_KEY': settings.STRIPE_PUBLISHABLE_KEY,
                                              'payment': payment},
                              context_instance=RequestContext(request))
Exemplo n.º 4
0
def pay_online(request, payment_id, template_name='payments/stripe/payonline.html'):
    with transaction.atomic():
        payment = get_object_or_404(Payment.objects.select_for_update(), pk=payment_id)
        form = StripeCardForm(request.POST or None)
        billing_info_form = BillingInfoForm(request.POST or None, instance=payment)
        currency = get_setting('site', 'global', 'currency')
        if not currency:
            currency = 'usd'
        if request.method == "POST":
            if form.is_valid():
                # get stripe token and make a payment immediately
                stripe.api_key = getattr(settings, 'STRIPE_SECRET_KEY', '')
                token = request.POST.get('stripe_token')

            if billing_info_form.is_valid():
                payment = billing_info_form.save()
                
            # determine if we need to create a stripe customer (for membership auto renew)
            customer = False
            obj_user = None
            membership = None
            obj = payment.invoice.get_object()
            if obj and hasattr(obj, 'memberships'):
                membership = obj.memberships()[0]
                if membership.auto_renew and not membership.has_rp(platform='stripe'):
                    obj_user = membership.user
                else:
                    membership = None
                    
            if obj_user:
                try:
                    # Create a Customer:
                    customer = stripe.Customer.create(
                                email=obj_user.email,
                                description="For membership auto renew",
                                source=token,
                    )
                except:
                    customer = None

            # create the charge on Stripe's servers - this will charge the user's card
            params = {
                       'amount': math.trunc(payment.amount * 100), # amount in cents, again
                       'currency': currency,
                       'description': payment.description
                      }
            if customer:
                params.update({'customer': customer.id})
            else:
                params.update({'card': token})

            try:
                charge_response = stripe.Charge.create(**params)
                # an example of response: https://api.stripe.com/v1/charges/ch_YjKFjLIItzRDv7
                #charge_response = simplejson.loads(charge)
            except stripe.error.CardError as e:
                # it's a decline
                json_body = e.json_body
                err  = json_body and json_body['error']
                code = err and err['code']
                message = err and err['message']
                charge_response = '{message} status={status}, code={code}'.format(
                            message=message, status=e.http_status, code=code)
            except Exception as e:
                charge_response = e.message
               
            # add a rp entry now 
            if hasattr(charge_response,'paid') and charge_response.paid:
                if customer and membership:
                    kwargs = {'platform': 'stripe',
                              'customer_profile_id': customer.id,
                              }
                    membership.get_or_create_rp(request.user, **kwargs)

            # update payment status and object
            if not payment.is_approved:  # if not already processed
                payment_update_stripe(request, charge_response, payment)
                payment_processing_object_updates(request, payment)

                # log an event
                log_payment(request, payment)

                # send payment recipients notification
                send_payment_notice(request, payment)

            # redirect to thankyou
            return HttpResponseRedirect(reverse('stripe.thank_you', args=[payment.id]))

    return render_to_response(template_name, {'form': form,
                                              'billing_info_form': billing_info_form,
                                              'STRIPE_PUBLISHABLE_KEY': settings.STRIPE_PUBLISHABLE_KEY,
                                              'payment': payment},
                              context_instance=RequestContext(request))