带有Stripe的Python / Django:创建Stripe订阅时,POST请求中出现错误

时间:2018-08-12 14:41:49

标签: python django stripe-payments

这是我第一次使用Stripe,这对任何人来说都很熟悉吗? 因此,我可以看到在付款时正在创建令牌,但是我没有将其重定向到我的模板,而是在下面抛出了此错误!

错误

# either this, to access keras.*, e.g., keras.datasets.imdb
import tensorflow.keras as keras

# or this, as you've done in your example
from tensorflow.keras.datasets import imdb

代码

>**InvalidRequestError:**
>Request req_d5BvUPtlpLrsG5: Received unknown parameter: source
>Request Method:    POST
>Django Version:    2.1
>Exception Type:    InvalidRequestError
>Exception Value:   
>**Request req_d5BvUPtlpLrsG5: Received unknown parameter: source**

2 个答案:

答案 0 :(得分:1)

您似乎需要在stripe.Subscription.create()方法中删除source = token键值对。

因此,您应该拥有更多类似的东西:

def PaymentView(request):

  user_membership = get_user_membership(request)

  selected_membership = get_selected_membership(request)

  publishKey = settings.STRIPE_PUBLISHABLE_KEY

  import stripe
  stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

  if request.method == "POST":
    try:
      token = request.POST['stripeToken']
      print(token) # WHAT IS PRINTED OUT HERE?
      subscription = stripe.Subscription.create(
        customer=user_membership.stripe_customer_id,
        items=[
          {
            "plan": selected_membership.stripe_plan_id,
          },
        ]
      )

      return redirect(reverse('memberships:update-transactions', kwargs={ 'subscription_id': subscription.id }))

    except stripe.error.CardError as e:
      messages.info(request, "Your card has been declined")

stripe.Subscription.create()对象方法的可接受参数为:客户(必需),application_fee_percent,计费,billing_cycle_anchor,优惠券,days_until_due,项目,元数据,按比例分配,tax_precent,trial_end,trial_from_plan和trial_period_days(所有可选) )。

这可能会有所帮助:https://stripe.com/docs/api/python#create_subscription

答案 1 :(得分:0)

我遍历了本教程,尝试解决此问题的方法是通过在Javascript中创建源对象而不是令牌对象。因此,如果您查看下面的JS代码(从STRIPE API文档复制),则可以看到其现在的createSource。source ==将附加到其帐户的CC号。

在Django中,我先创建了stripe.Customer.create_source,然后在将卡保存到帐户后创建了stripe.Subscription.create。由于Stripe Subscriptions中的默认设置是在卡附带客户的情况下自动对订阅进行收费帐户应立即从卡中扣除。

只需获取source_id,就像您在Django中从表单获取令牌的方法一样,然后将其传递给stripe.Customer.create_source(显然,请获取您的Stripe客户ID来传递驴)。

  // Create an instance of the card Element.
  var card = elements.create('card', {style: style});

  // Add an instance of the card Element into the `card-element` <div>.
  card.mount('#card-element');

  // Handle real-time validation errors from the card Element.
  card.addEventListener('change', function(event) {
    var displayError = document.getElementById('card-errors');
    if (event.error) {
      displayError.textContent = event.error.message;
    } else {
      displayError.textContent = '';
    }
  });

  // Handle form submission.
  var form = document.getElementById('payment-form');
  form.addEventListener('submit', function(event) {
    event.preventDefault();

    form.addEventListener('submit', function(event) {
    event.preventDefault();

    stripe.createSource(card).then(function(result) {
      if (result.error) {
        // Inform the user if there was an error
        var errorElement = document.getElementById('card-errors');
        errorElement.textContent = result.error.message;
      } else {
        // Send the source to your server
        stripeSourceHandler(result.source);
      }
    });
  });
});
if request.method == 'POST':
        try:
            source = request.POST['stripeSource']
            stripe.Customer.create_source(
                user_membership.stripe_customer_id,
                source=source)

            stripe.Subscription.create(
                customer=user_membership.stripe_customer_id,
                items=[
                    {
                    "plan": selected_membership.stripe_plan_id,
                    },
                ])
            messages.success(request, 'Your payment was completed!')    
            # stripe.Customer.create_source(type='')
        except stripe.CardError as e:
            messages.info(request, 'Your card has been declined!')

注意:根据他们的文档,您可以在JS对象中传递客户数据(地址等),如下所示。您可能会添加到表单中,以使用户输入信用卡地址数据等,以便在需要时将其传递到其条纹客户帐户中。

// Create a source or display an error when the form is submitted. 
var form = document.getElementById('payment-form');
var ownerInfo = {
  owner: {
    name: 'Jenny Rosen',
    address: {
      line1: 'Nollendorfstraße 27',
      city: 'Berlin',
      postal_code: '10777',
      country: 'DE',
    },
    email: 'jenny.rosen@example.com'
  },
};
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createSource(card, ownerInfo).then(function(result) {
  //rest of stripe js code.....

希望这会有所帮助!