我和Stripe挣扎。我正在使用PHP而我正试图建立一个没有CMS的简单商店。想知道如何将金额转入charge.php,这样我可以为不同的产品收取不同的金额。这是我的代码:
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 1900;,
'currency' => 'gbp'
));
这里是index.php的代码 - 我想向客户收取“#data; data-amount"在下面的表格上。不太清楚该怎么做。
<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 :(得分: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
}
?>
答案 1 :(得分:0)
为什么要在data-amount
中收取 的费用?你从哪里获得这个价值? data-amount告诉Stripe用户允许您充电的内容。 Stripe_Charge :: create中的金额是您实际收取的金额。
您可以使用与data-amount相同的值填充隐藏的输入字段。但我不知道你会从中获得什么。 你的PHP skript应该计算支付金额。不要相信客户。他可以将数据量的值改为支付更少,即50。 使用以下费用可以使用,但客户支付50便士而不是1900。
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => $_POST['hidden_amount']
向Stripe询问总计算付款额。如果客户端搞乱了数据量,则收费将失败。
'amount' => $shoppingcart->getTotal();,