The goal: 我有一个html页面,你可以购买两个不同的项目 . 一个是5美元,另一个是10美元 . 我'm using stripe to process the payments. When stripe creates a payment token and submits that to my process_charge.php I'喜欢从POST提交的数据中提取费用 . 然后我可以使用该金额来调用 Stripe_Charge::create()
.
The problem: 金额不是't being submitted to my PHP form and I'我不知道如何获得它 .
My temporary solution: 我正在销售的每件商品都有不同的php表单 . 然后为每个新项目提供一个新的PHP表格!不理想
我目前正在使用此处列出的嵌入式表单方法:https://stripe.com/docs/tutorials/checkout
我的订单页面看起来像这样 .
<div class="payment-options">
<form action="process_card.php" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_PUBLIC_KEY_FOR_TESTING"
data-amount="1000"
data-name="Nice Lint"
data-description="High quality belly button lint."
data-image="images/nice-lint.svg"
data-panel-label="Checkout {{amount}}"
data-label="Pay with Credit Card"
data-billing-address="true"
data-shipping-address="true">
</script>
</form>
</div>
所以这很有效 . 它包含客户的信息,运输/账单地址和信用卡详细信息 . 然后它生成一个充电令牌并将其提交给 process_card.php
. 在那里,我可以获取令牌并执行以下操作:
if ($_POST) {
Stripe::setApiKey("sk_test_SECRET_KEY_FOR_TESTING");
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
Stripe_Charge::create(array("amount" => '1000'],
"currency" => "usd",
"card" => $_POST['stripeToken']));
}
catch (Exception $e) {
$error = $e->getMessage();
}
}
在这里,我获取通过 POST
提交的令牌,并使用他们的API将其发送到条带 . 金额,货币和卡令牌是必填字段 . 您可以看到令牌是从 POST
数据中提取的,但是我必须为该金额发送一个文字字符串 . 这一切都很好,花花公子,直到我有多个产品/服务,我有不同的形式生成不同数量的令牌,并将它们发送到我的 process_card.php
. 那个文字字符串不再看起来很开心了 . 他愚蠢的假笑从他愚蠢的脸上消失了 .
所以无论如何,我检查了所有 POST
数据,看看是否有任何提交的字段包含金额,所以我可以传递它 . 所以我只是做了一个循环来看看提交的内容:
foreach ($_POST as $key => $value) {
echo "key: " . $key . " - " . $value . "<br>";
}
它给了我这个:
key: stripeToken - tok_youShouldDefinitelyPostThisTokenOnAPublicWebsite
key: stripeEmail - super.real.email@aol.com
key: stripeBillingName - asdf
key: stripeBillingAddressLine1 - asdf
key: stripeBillingAddressZip - 12345
key: stripeBillingAddressCity - Schenectady
key: stripeBillingAddressState - NY
key: stripeBillingAddressCountry - United States
key: stripeBillingAddressCountryCode - US
key: stripeShippingName - asdf
key: stripeShippingAddressLine1 - asdf
key: stripeShippingAddressZip - 12345
key: stripeShippingAddressCity - Schenectady
key: stripeShippingAddressState - NY
key: stripeShippingAddressCountry - United States
key: stripeShippingAddressCountryCode - US
没有金额 . 哪里哪里 . 所以关闭API文档,看看我是否能找到解决方案 . 我能找到的最接近的是 Stripe_Charge::retrieve({CHARGE_ID});
,这里找到了:https://stripe.com/docs/api/php#retrieve_charge . 然而,这仅适用于成功处理的费用 .
所以我'm stuck. Can' t弄清楚了 . 我能想到的最好的解决方案是在我提供的嵌入式表单中添加一条 <input type="hidden" name="amt" value="1000" />
行,这样我就可以通过 $_POST['amt']
拉取金额 . 这个问题是我不知道它是否's PCI compliant. In many places on the stripe site/api/docs they tell you not to include name fields in their forms so you aren' t在你网站上的页面之间传递敏感信息,所以我想"better safe than sorry" . 有没有其他方法可以处理这个或我不知道的最佳实践?
**对不起,这太久了 . 我尽可能多地努力尝试自己解决它,我想提供尽可能多的信息 .
1 回答
我没有看到任何关于PCI合规性的问题,只要将其价格放在表格中(只要您使用SSL) . 然而,这将允许他们在提交之前简单地编辑HTML输入并使项目比您希望的更便宜 . 您可以通过使用带有products表的数据库并在隐藏的输入字段中引用产品ID来解决此问题 .