我正在尝试将令牌从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 后的响应是成功:后跟空格。有人知道出了什么问题吗?
有两件事解决了这个问题:
将我的 PHP 文件中的 API 密钥更改为正确的密钥,并将"金额"类型从整数调整为字符串:
func postToken(token:STPToken) {
let requestString = "https://thawing-inlet-46474.herokuapp.com/charge.php"
let params = ["stripeToken": token.tokenId, "amount": "200", "currency": "usd", "description": "testRun"]
//POST request (simple)
//Alamofire.request(.POST, requestString, parameters: params)
//POST request (with handler)
Alamofire.request(.POST, requestString, parameters: params)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: '(JSON)")
}
}
}