2014-05-16 103 views
4

我正在用Stripe掙扎,我正在使用PHP,我試圖建立一個簡單的商店,沒有CMS,想知道如何將金額傳遞給charge.php,以便我可以收取費用。不同量爲不同的產品這裏是我的代碼:帶條紋的動態付款

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

下面是index.php文件的代碼 - 我想給客戶無論是在下面的表格中的「數據量」收費不太清楚如何這樣做。

<form action="inc/charge.php" method="POST"> 
    <script 
      src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="<?php echo $stripe['publishable_key']; ?>" 
      data-amount="1900" 
      data-currency="GBP" 
      data-name="Pure - Tumblr Theme" 
      data-allow-remember-me="false" 
      data-description="Premium Tumblr Theme" 
      data-image="/128x128.png"> 
      </script> 
</form> 

回答

0

你爲什麼要收費任何是在data-amount?你從哪裏得到這個價值?數據量告訴Stripe用戶允許您收費的內容。 Stripe_Charge :: create中的金額是您實際收取的金額。

您可以使用與數據量相同的值填充隱藏的輸入字段。但我不知道你會從中得到什麼。 你的PHP skript應該計算要支付的金額。不要相信客戶。他可以改變數據量的值少付,即50 使用下面的電荷會的工作,但客戶支付50便士,而不是1900年

$charge = Stripe_Charge::create(array( 'customer' => $customer->id, 'amount' => $_POST['hidden_amount']

詢問條紋總量計算支付。如果客戶與數據量混淆,則收費將失敗。 'amount' => $shoppingcart->getTotal();,

3

更全面的,從index.php到charge.php,而不是相反。

<?php 
#set your variables 
$amount  = 500; 
$name   = 'My Company'; 
$currency  = 'gbp'; 
$description = 'Value Plan'; 
$uid   = get->your->uid; 
$email  = get->your->email; 
?> 

<center><form action="../charge.php" method="post"> 
<!-- make these hidden input types for the post action to charge.php --> 
<input type="hidden" name="amount"  value="<?php echo $amount?>"> 
<input type="hidden" name="name"  value="<?php echo $name;?>"> 
<input type="hidden" name="currency" value="<?php echo $currency;?>"> 
<input type="hidden" name="description" value="<?php echo $description;?>"> 
<input type="hidden" name="uid"   value="<?php echo $uid;?>"> 
<input type="hidden" name="email"  value="<?php echo $email;?>"> 

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 

     data-key =   "<?php echo $stripe['publishable_key']; ?>" 
     data-amount =  "<?php echo $amount;?>" 
     data-name =   "<?php echo $name;?>" 
     data-currency =  "<?php echo $currency;?>" 
     data-description = "<?php echo $description;?>" 
     data-email =   "<?php echo $user->data()->email; ?>" 
     data-billing-address =  "true" 
     data-allow-remember-me = "false" 
     > 

</script> 
</form></center> 

然後在charge.php可以調用輸入值你的index.php藏

<?php 
$token  = $_POST['stripeToken']; 
$email  = $_POST['email']; 
$uid   = $_POST['uid']; 
$currency  = $_POST['currency']; 
$amount  = $_POST['amount']; 
$description = $_POST['description']; 

#This is the standard try catch block stripe suggests 
try{ 
$charge = Stripe_Charge::create(array(
"amount"  => $amount, 
"currency"  => $currency, 
"customer"  => $charge_to, 
"description" => $description 
)); 

} catch(Stripe_CardError $e) { 

$error = $e->getMessage(); 
// Since it's a decline, Stripe_CardError will be caught 
$body = $e->getJsonBody(); 
$err = $body['error']; 

print('Status is:' . $e->getHttpStatus() . "\n"); 
print('Type is:' . $err['type'] . "\n"); 
print('Code is:' . $err['code'] . "\n"); 
// param is '' in this case 
print('Param is:' . $err['param'] . "\n"); 
print('Message is:' . $err['message'] . "\n"); 
} catch (Stripe_InvalidRequestError $e) { 

// Invalid parameters were supplied to Stripe's API 
} catch (Stripe_AuthenticationError $e) { 
// Authentication with Stripe's API failed 
// (maybe you changed API keys recently) 
} catch (Stripe_ApiConnectionError $e) { 
// Network communication with Stripe failed 
} catch (Stripe_Error $e) { 
// Display a very generic error to the user, and maybe send 
// yourself an email 
} catch (Exception $e) { 
// Something else happened, completely unrelated to Stripe 
} 
?> 
+0

感謝這真的幫了我 – DCorrigan