2014-12-30 2 views
0

아래 코드는 내 Stripe Subscription Payments에 사용하는 코드입니다.이 사이트를 구현하고있는 것은 AngularJS입니다.이 AJAX를 사용하기 위해 Refreshing 사이트를 유지하고 싶습니다. 선택권.Stripe AJAX PHP와 JavaScript

나는 내가 이것을 제외하면

$charge = Stripe_Charge::create(array(
    'customer' => $customer->id, 
    'amount' => $amount, 
    'currency' => 'gbp' 
)); 

인 PHP의 조각을 주석 한 지불 한 번 통과하고 나는이 응용 프로그램에는이 경우 charge을 처리하지 할 수있는 방법을 확실 해요 PHP 파일에서 호출하십시오.

상기 스 니펫이 포함 된 경우 요금이 두 번 적용됩니다.

config.php에는 내 API 키와 함께 require_once('Stripe.php'); 만 있습니다.

그래서 누군가가 내 코드가 계속 정상적으로 계속 진행될 경우 요금 코드 조각이없는 이유를 설명 할 수 있기를 바랍니다.

HTML

<button id="proBtn">Subscribe</button> 

자바 스크립트

Stripe.setPublishableKey('HIDDEN'); 

    $('#proBtn').click(function(){ 
     var token = function(res){ 
      var $input = $('<input type="hidden" name="stripeToken" />').val(res.id); 
      var tokenId = $input.val(); 
      var email = res.email; 

      setTimeout(function(){ 
       $.ajax({ 
        url:'/assets/lib/charge.php', 
        cache: false, 
        data:{ stripeEmail : email, stripeToken:tokenId, stripePlan: 'pro' }, 
        type:'POST' 
       }) 
       .done(function(data){ 
        // If Payment Success 
        console.log(data); 
        $('#proBtn').html('Thank You').addClass('disabled'); 
       }) 
       .error(function(){ 
        $('#proBtn').html('Error, Unable to Process Payment').addClass('disabled'); 
       }); 
      },500); 
      //$('form:first-child').append($input).submit(); 
     }; 

     StripeCheckout.open({ 
      key:   'HIDDEN', // Your Key 
      address:  false, 
      amount:  500, 
      currency: 'gbp', 
      name:  'Pro Account', 
      description: '', 
      panelLabel: 'Checkout', 
      allowRememberMe: false, 
      token:  token 
     }); 
     return false; 
    }); 

charge.php

<?php 
    require_once($_SERVER['DOCUMENT_ROOT'].'/assets/lib/config.php'); 

    $token = $_POST['stripeToken']; 
    $email = $_POST['stripeEmail']; 

    $plan = $_POST['stripePlan']; 

    if ($plan == 'pro') { 
     $amount = 500; 
     $amountFormat = number_format($amount/100, 2) ; 
     $plan = 'pro'; 
    } 

    if ($plan == 'team') { 
     $amount = 2000; 
     $amountFormat = number_format($amount/100, 2) ; 
     $plan = 'team'; 
    } 

    Stripe_Plan::retrieve("pro"); 
    Stripe_Plan::retrieve("team"); 

    $customer = Stripe_Customer::create(array(
     'email' => $email, 
     'card' => $token, 
     'plan' => $plan 
)); 

try { 
    /* 
    $charge = Stripe_Charge::create(array(
     'customer' => $customer->id, 
     'amount' => $amount, 
     'currency' => 'gbp' 
    ));*/ 
    echo 'success'; 
} catch(Stripe_CardError $e) { 
    echo "The card has been declined"; 
    echo $token; 
} 

    print_r($token); 
    echo '<br/>'; 
    print_r($email); 
    echo '<br/>'; 
    echo $customer->id; 

    echo '<h1>Successfully charged '.$amountFormat.'!</h1>'; 
?> 
+0

왜 당신이 버튼을 클릭 거기에의 setTimeout 필요합니까 :

다음은 고객의 API에 대한 링크입니까? – Rupert

+0

충전 작업을 실행하기 전에 데이터 검증/위생을 추가해야합니다. '$ _POST'에있는 입력 데이터가 쓸데없는 것으로 믿을 수는 없습니다. –

답변

2

사용자 정의를 만들 스트라이프에서는 계획을 통과하면 요금이 자동 청구됩니다. 기본적으로이 고객을 만들고이 계획에 서명하도록 말하고 있습니다. 그래서 두 번 요금을 부과합니다.

https://stripe.com/docs/api#create_customer