2017-01-12 8 views
2

同じ顧客を複数回作成することを避けるために、顧客IDを含むテーブルapp__stripe_customerを作成しました。リロードページのお客様のストライプが再び請求されます

if ($_POST) { 

    \Stripe\Stripe::setApiKey($StripeKeySecret); 
    $error = ''; 
    $success = ''; 

    /** 
    * Check if Customer Exists if not Create a Customer: 
    */ 
    try { 
     $sql = $dataBase->prepare('SELECT * FROM app__stripe_customer 
            WHERE user_id = :uid'); 
     $sql->execute(array('uid' => $_SESSION['user_id'])); 
     $stripeCustomer = $sql->fetch(); 
     if(empty($stripeCustomer)) { 
      /** 
      * We create the new Stripe Customer 
      */ 
      $customer = \Stripe\Customer::create(array(
       "email" => $user['email'], 
       "source" => $token)); 

      /** 
      * Creating new Stripe Customer Id in database 
      */ 
      $sql = $dataBase->prepare('INSERT INTO app__stripe_customer(user_id, customer_id) 
             VALUES(:uid, 
               :cid)'); 
      $sql->execute(array('uid' => $_SESSION['user_id'], 
           'cid' => $customer->id)); 
      $stripeCustomerId = $customer->id; 
     } else { 
      $stripeCustomerId = $stripeCustomer['customer_id']; 
     } 

     if (!isset($_POST['stripeToken'])) 
      throw new Exception("The Stripe Token was not generated correctly"); 
     $charge = \Stripe\Charge::create(array("amount" => $AMT*100, 
               "currency" => "usd", 
               "customer" => $stripeCustomerId)); 
     $chargeID = $charge->id; 
     $success = 'Your payment was successful: '.$chargeID; 
     //echo $success; 
     show__paymentDone(); 

    } catch (Exception $e) { 

     $error = $e->getMessage(); 

     show__errorPayment($error); 

    } 

} 

お客様が存在する場合、トークンは使用されず、ユーザーがページをリロードすると、再び請求されます。

私には、このコードは問題なく見えますが、ユーザーに何回も料金を請求するのを防ぐにはどうすればよいですか?

+0

使用https://en.wikipedia.org/wiki/Post/Redirect/Get – ceejayoz

答えて

1

if($_POST)$_SESSIONを使用しての方法:

if((isset($_SESSION['stripe_token']) && ($_SESSION['stripe_token'] == $_POST['stripeToken'])) { 
     show__errorTokenTwice($token); 
     exit; 
} 

充電が完了した後:

$_SESSION['stripe_token'] = $_POST['stripeToken'] 
関連する問題