我已经构建了一个Django视图,以便我可以通过AJAX调用验证Stripe优惠券代码,但我还没有得到正确的代码。
我知道视图至少会被触发,因为我放在那里的print语句有效,但遗憾的是它显示coupon_code为None。此外,当我在输入优惠券代码(有效或无效)后点击“应用”按钮时,我得到GET http://localhost:8000/payments/coupon 404(未找到)。我想也许我应该将AJAX更改为POST请求,但后来我得到405(METHOD NOT NOTOWED)。
我应该使用GET还是POST?如何成功获取优惠券代码以进行验证?我需要做些什么才能将视图中的Stripe结果返回到模板中,以便我可以在结帐过程中应用百分比或金额?
以下是我的观点:
def register_extensions(app):
security.init_app(app, datastore=ds, register_form=forms.ExtendedRegisterForm)
class ExtendedRegisterForm(RegisterForm):
pay_month = SelectField(choices=[('need', 'custom'), ('day', 'choices')])
以下是我的urls.py中的适用代码:
class StripeCouponView(generic.View):
def get(self, request):
stripe.api_key=settings.STRIPE_SECRET_KEY
if request.method == 'GET':
coupon_code = request.GET.get('coupon_code')
print "coupon_code IS", coupon_code
try:
coupon_response = stripe.Coupon.retrieve(coupon_code)
return HttpResponse(
json.dumps(coupon_response),
content_type="application/json"
)
except (requests.exceptions.HTTPError, Exception):
return HttpResponseNotFound('BAD request')
else:
return HttpResponseNotFound('No coupon?')
这是HTML(由djstripe's subscribe_form.html构建):
from .views import StripeCouponView
# ... a lot of URLs
url(r'^payments/coupon',
StripeCouponView.as_view(),
name='stripecoupon'),
这是发生Stripe结帐操作的JavaScript:
{% extends "djstripe/base.html" %}
{% load static djstripe_tags %}
{% block title %}Subscribe{% endblock title %}
{% block content %}
<div class="row">
{% for plan in PLAN_LIST %}
{% with plan_count=PLAN_LIST|length %}
<div class="col-xs-{{ 6|djdiv:plan_count|floatformat }} col-xs-offset-3">
{% endwith %}
<form
{% if not customer.current_subscription or customer.current_subscription.status == CurrentSubscription.STATUS_CANCELLED %}
action="{% url 'djstripe:subscribe' %}" class="djstripe-subscribe"
data-key="{{ STRIPE_PUBLIC_KEY }}"
data-amount="{{ plan.price }}"
data-name="{{ plan.name }}"
data-description="{{ plan.description }}"
{% else %}
data-stripe-key="{{ STRIPE_PUBLIC_KEY }}"
action="{% url 'djstripe:change_plan' %}" class="djstripe-change-plan"
{% endif %}
method="POST">
{% csrf_token %}
<input type="hidden" name="plan" value="{{ plan.plan }}" />
<input name="stripe_token" type="hidden" />
<!-- disable this when clicked -->
<button style="width:100%; border-radius:4px;"
{% if customer.current_subscription.plan == plan.plan and customer.current_subscription.status != CurrentSubscription.STATUS_CANCELLED %}
disabled="true"
{% endif %}
type="submit" class="btn btn-primary">
{% comment %}
{% with image=plan.image|default:"img/default-plan-image.png" %}
<img src="{% static image %}" class="img-thumbnail" />
{% endwith %}
{% endcomment %}
<h3>{{ plan.name }}</h3>
<p>{{ plan.description }}</p>
</button>
{% if not customer.current_subscription or customer.current_subscription.status == CurrentSubscription.STATUS_CANCELLED %}
<!-- do nothing -->
{% elif customer.current_subscription.plan == plan.plan %}
<h4>Your Current Plan</h4>
{% endif %}
</form>
<span class="discount-span"><label>Discount Code:</label>
<input type="text" id="discount-field"/>
<button id="apply-coupon">Apply</button></span>
<div id="coupon-msg">
</div>
</div>
{% endfor %}
</div>
{% endblock content %}