在strip-charg中传递php变量
passing php varible in strip charg
我想在stripe中传递php变量,但它们显示缺少参数数量的错误。
这是我的代码,我从上一页得到这个金额,想在这个代码中传递金额变量,但我在网上搜索,他们说只传递美分值。有什么解决方案可以通过变量传递金额吗?我想动态传递值,而不是静态传递值,这怎么可能呢?
$amountvalue = '123';
try {
if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
throw new Exception("Fill out all required fields.");
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
Stripe_Charge::create(array("amount" => $amountvalue,
"currency" => "aud",
"card" => $_POST['stripeToken'],
"description" => $_POST['email']));
$success = '<div class="alert alert-success">
<strong>Success!</strong> Your payment was successful.
</div>';
}
catch (Exception $e) {
$error = '<div class="alert alert-danger">
<strong>Error!</strong> '.$e->getMessage().'
</div>';
}
但Stripe似乎不会排除这个变量。它在页面上放置了一个"缺少必需参数:金额"错误。它只想接受一个实际的整数,如"1234"
在此处输入图像描述
根据您的说法,如果Stripe_Charge::create()静态方法只需要Numeric数据而不需要String;那么你也可以简单地输入数字数据,比如
<?php
// THIS IS EXACTLY THE SAME CODE YOU SUPPLIED ABOVE WITH JUST A SIMPLE CHANGE:
// THIS TIME WE ARE USING CENTS RATHER TO MAKE IT EASIER.
$amountvalue = '123';
try {
if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
throw new Exception("Fill out all required fields.");
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
// NOTICE THAT THE ONLY CHANGE HERE IS TO WRAP THE $amountvalue INSIDE OF intval()
// THIS CONVERTS THAT $amountvalue TO AN INTEGER,
// REGARDLESS OF WHETHER IT IS DYNAMIC OR HARD-CODED AS IT IN THIS CASE.
// NOTICE ALSO THAT WE ARE MULTIPLYING THE PASSED AMOUNT BY 100
// THAT IS CONVERTING THE $amountvalue TO CENTS...
Stripe_Charge::create(array("amount" => intval($amountvalue)*100,
"currency" => "aud",
"card" => $_POST['stripeToken'],
"description" => $_POST['email']));
$success = '<div class="alert alert-success">
<strong>Success!</strong> Your payment was successful.
</div>';
}
catch (Exception $e) {
$error = '<div class="alert alert-danger">
<strong>Error!</strong> '.$e->getMessage().'
</div>';
}
Stripe要求您传递一个源参数,其中包含信用卡数据,如下所示:
'Stripe'Charge::create(array(
"amount" => 400,
"currency" => "usd",
// YOU'RE NOT PASSING THIS source ARRAY PARAMETER...
// YOU'RE PASSING JUST card INSTEAD... PROBABLY THE CREDIT-CARD NR. ONLY
"source" => array(
"number" => "4242424242424242",
"exp_month" => 5,
"exp_year" => 2017,
"cvc" => "314"
),
"description" => "Charge for test@example.com"
));
参考:https://stripe.com/us/features
如果你的函数真的NUMBER HUNGRY,它会微笑着改变…;-)
干杯&祝你好运
问题似乎不是数量,而是在创建函数中传递的数据。看看这个代码:
<?php
// THIS IS EXACTLY THE SAME CODE YOU SUPPLIED ABOVE WITH JUST A SIMPLE CHANGE:
// THIS TIME WE ARE USING CENTS RATHER TO MAKE IT EASIER.
$amountvalue = '123';
try {
if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
throw new Exception("Fill out all required fields.");
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
Stripe_Charge::create(array("amount" => intval($amountvalue)*100,
"currency" => "aud",
//HERE WAS THE ISSUE:
'source' => array(
"number" => $_POST['stripeToken'],
"exp_month" => $_POST['expiry_month'], // GET THE CORRESPONDING POST VALUE FOR EXPIRY MONTH
"exp_year" => $_POST['expiry_year'], // GET THE CORRESPONDING POST VALUE FOR EXPIRY MONTH
"cvc" => $_POST['cvc'] // GET THE CORRESPONDING POST VALUE FOR EXPIRY MONTH
),
// REMOVE THIS ENTRY: "card"
// "card" => $_POST['stripeToken'],
"description" => $_POST['email']));
$success = '<div class="alert alert-success">
<strong>Success!</strong> Your payment was successful.
</div>';
}
catch (Exception $e) {
$error = '<div class="alert alert-danger">
<strong>Error!</strong> '.$e->getMessage().'
</div>';
}
您只需要一种获取和传递信用卡信息的方法:信用卡号、到期月、到期年和CVC。我相信这会解决你的问题。。。
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 通过javascript重定向html传递php变量
- 如何在php变量中嵌入JQuery代码
- 在javascript函数中使用php变量
- 将地理编码结果转换为php变量以发布到mysql数据库
- 如何在提交表单时将PHP变量传递到Javascript cookie中
- 从Javascript和Php变量创建Html模板文档
- 如何将php变量作为javascript中列表的元素
- 将PHP变量分配给PHP数组
- 如何在没有ajax的情况下将javascript动态数据发送到php变量
- 在Javascript甜甜圈图的PHP变量中引用PHP变量
- 使用javascript访问php变量
- 在Javascript文件中获取PHP变量
- 如何为Javascript数组()的使用准备PHP变量
- 如何获取PHP变量数据并将其存储在JavaScript变量中
- Javascript变量到PHP变量-如果语句不起作用,则发布
- 通过Javascript验证PHP变量
- 如何将Ajax数据值分配给PHP变量
- 如何在 bootsrap 完整日历中使用 PHP 变量
- 在javascript中调用PHP变量