2017-01-28 4 views
1

私はストライプを使用してチェックアウト決済フォームを作成する段階にあります。私は月と年をフォーマットするためにjquery.paymentライブラリを使用していますhttps://github.com/stripe/jquery.paymentストライプ - 'exp_month'パラメータは、Djangoビューの整数(02は代わりに)にする必要があります

私はデータを収集するためにexp入力フィールドを作成したが、ストライプを得るために、私は新しい変数としてそれらを作成し、私のexpフィールドの値を取り、私は必要な値をスライスし、その後、渡さexp_monthexp_yearフィールドを必要と私の見解にそれらを。

正しい値が私のビューに渡されていますが、stripe.Token.createに達すると、コンソールでstripe.error.CardError: Request req_A16AvRkITlz3Oi: The 'exp_month' parameter should be an integer (instead, is 02).エラーが発生します。サイドノート:このフォームは、exp_monthexp_yearに固有のフィールドを持っていれば完璧に機能しましたが、どれだけ楽しいですか?以下は:)

レビューのために私のコードです:それはint型に変換する必要がありますので

アヤックス

var fields = { 
    number: 'null', 
    cvc: 'null', 
    exp: 'null', 
    exp_month: 'null', 
    exp_year: 'null' 
}; 

$('#plaqueFormBTN').click(function (e) { 
    e.preventDefault(); 
    fields.number = $('#id_number').val(); 
    fields.exp = $('#id_exp').val(); 
    fields.cvc = $('#id_cvc').val(); 

    $('#loader').show(); 

    var exp_month = fields.exp.slice(0, 3); 
    var exp_year = fields.exp.slice(5); 
    console.log(exp_month); 
    console.log(exp_year); 
    $.ajax({ 
     type: 'POST', 
     data: { 
      csrfmiddlewaretoken: getCookie('csrftoken'), 
      number: fields.number, 
      exp: fields.exp, 
      exp_month: exp_month, 
      exp_year: exp_year, 
      cvc: fields.cvc 

     }, 
     url: $('#plaqueForm').attr('action'), 
     cache: false, 
     ... 

ビュー

def plaque_order_form(request): 
    if request.method == 'POST': 
     form = PlaqueOrderForm(data=request.POST) 
     if form.is_valid(): 
      number = request.POST.get('number', '') 
      cvc = request.POST.get('cvc', '') 
      exp_month = request.POST.get('exp_month', '') 
      exp_year = request.POST.get('exp_year', '') 

      subject = 'New Plaque Order' 
      from_email = settings.DEFAULT_FROM_EMAIL 
      recipient_list = [from_email] 

      # Token is created using Stripe.js or Checkout! 
      # Get the payment token submitted by the form: 

      token = stripe.Token.create(
       card={ 
        'number': number, 
        'exp_month': exp_month, 
        'exp_year': exp_year, 
        'cvc': cvc 
       }, 
      ) 
      ... 

答えて

2

'02'は文字列ですストライプに入る前にあなただけ行う必要があります:

 exp_month = int(request.POST.get('exp_month', '')) 
     exp_year = int(request.POST.get('exp_year', '')) 
0

あなたはPOSTから直接form.cleaned_data辞書を介してデータにアクセスするのではなくする必要があります。とりわけ、このフォームは適切なデータ型に変換する処理を行います。

+0

「int」に変換された@danyamachineは言及されましたが、私はあなたに清掃されたデータを使用する必要があることに同意しますが、私はいつも問題なく確実に動作するようになっています。フィールドを 'exp_year = form.cleaned_data.get( 'exp_year')'のように書式を変更し、 'stripe.error.InvalidRequestError:Request req_A1C8GvSrpG3It3:Missing required param:exp_year 'というエラーが出ました。 ' –

関連する問題