我正在努力使用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>
解决方法: 更全面,从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
}
?>
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|