1

This is my first time using stripe does this look familiar to anyone? So I am able to see that a token is being created upon payment, but instead of being redirected to my template, i throws this error below!

ERROR

>**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**

CODE

def PaymentView(request):

    user_membership = get_user_membership(request)

    selected_membership = get_selected_membership(request)

    publishKey = settings.STRIPE_PUBLISHABLE_KEY

    if request.method == "POST":
        try:
            token = request.POST['stripeToken']
            subscription = stripe.Subscription.create(
              customer=user_membership.stripe_customer_id,# id on User Membership Model
              items=[
                {
                  "plan": selected_membership.stripe_plan_id,
                },
              ],
              source=token # 4242424242424242
            )

            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")

    context = {
        'publishKey': publishKey,
        'selected_membership': selected_membership
    }

    return render(request, "path/templategoeshere.html", context)

2 Answers 2

1

It looks like you need to remove the source=token key value pair in the stripe.Subscription.create() method.

So, you should have something more like:

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")

The accepted arguments for the stripe.Subscription.create() object method are: customer (required), application_fee_percent, billing, billing_cycle_anchor, coupon, days_until_due, items, metadata, prorate, tax_precent, trial_end, trial_from_plan and trial_period_days (all optional).

This may help: https://stripe.com/docs/api/python#create_subscription

Sign up to request clarification or add additional context in comments.

15 Comments

thats weird im watching i video that was made on july 9, 2018 and he is literally using it and making it work. Infact here is the link to his views page. github.com/justdjango/video-membership/blob/master/memberships/…
and here is the video, go to 2hrs in the video and start watching
Yes - how odd! Did you try it with it removed? As unlikely as it sounds - it may have changed in the intervening time. Could you do some quick print debug for me and use the following: print(token) after token is declared from the POST['stripeToken'] and then immediately print(type(token)) for me...
What version of the API are you using? Did you upgrade at all? It looks like the source param on this call was deprecated in the latest version. stripe.com/docs/upgrades#2018-07-27
@teaglebuilt See the above comment from "duck" - it looks like it was deprecated in the last version. Did everything work ok with the changes I suggested?
|
0

I went through the tutorial and the way I tried to solve this was by creating a source object instead of token object in the Javascript. So if you look at JS code below (copied from STRIPE API docs) you can see its now createSource.The source == the CC number which will be attached to their account.

Back in Django I create a stripe.Customer.create_source and then subsequently the stripe.Subscription.create after I have the card saved to account.. Since the default in Stripe Subscriptions is to auto charge subscription with the card attached to customer account it should charge the card immediately.

Just grab the source_id similar to how you did with token from the form in Django and then pass into stripe.Customer.create_source (obviously grab your Stripe customer id to pass in ass well).

  // 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!')

Note: Per their docs you can pass in the customer data (address, etc) in the JS object like below. You could maybe add to the form to have user enter in credit card address data etc so it passes into their stripe customer account if you want to.

// 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: '[email protected]'
  },
};
form.addEventListener('submit', function(event) {
  event.preventDefault();

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

Hopefully this helps!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.