【发布时间】:2016-08-17 07:03:34
【问题描述】:
我正在尝试从 iOS 中的 Alamofire 将令牌发布到 PHP 文件 (https://thawing-inlet-46474.herokuapp.com/charge.php),但 Stripe 没有显示任何已完成的费用。这是准确的 PHP 文件:
<?php
require_once('vendor/autoload.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_qCTa2pcFZ9wG6jEvPGY7tLOK");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$amount = $_POST['amount'];
$currency = $_POST['currency'];
$description = $_POST['description'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount*100, // Convert amount in cents to dollar
"currency" => $currency,
"source" => $token,
"description" => $description)
);
// Check that it was paid:
if ($charge->paid == true) {
$response = array( 'status'=> 'Success', 'message'=>'Payment has been charged!!' );
} else { // Charge was not paid!
$response = array( 'status'=> 'Failure', 'message'=>'Your payment could NOT be processed because the payment system rejected the transaction. You can try again or use another card.' );
}
header('Content-Type: application/json');
echo json_encode($response);
} catch(\Stripe\Error\Card $e) {
// The card has been declined
header('Content-Type: application/json');
echo json_encode($response);
}
?>
这是来自 Swift 的代码:
func postToken(token:STPToken) {
let parameters : [ String : AnyObject] = ["stripeToken": token.tokenId, "amount": 10000, "currency": "usd", "description": "testRun"]
Alamofire.request(.POST, "https://thawing-inlet-46474.herokuapp.com/charge.php", parameters: parameters).responseString { (response) in
print(response)
}
}
肯定会创建令牌,发布到 Heroku 后的响应是 SUCCESS: 然后是空格。。有人知道出了什么问题吗?
【问题讨论】:
-
1/ 捕获所有错误,而不仅仅是卡片错误。 2/ 在仪表板中查看您的日志:dashboard.stripe.com/logs。 3/ 查看服务器上的错误日志。 99% 确定您在 iOS 应用程序中使用了错误的 API 密钥,这是最常见的错误。
-
谢谢!除了更改参数类型之外,我实际上确实需要更改 API 密钥。
标签: php ios heroku token stripe-payments