我目前正将我的IOS应用与后端相关联以处理Stripe付款。我使用FileZilla来托管文件,使用cSpan来托管服务器。这是我的快速代码:
let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, errorD -> Void in
if errorD == nil {
print("got response")
do {
let jsonResult: NSDictionary? = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
let jsonComp = jsonResult?.value(forKey: "completion") as! String
print(jsonComp)
} catch {
print(errorD?.localizedDescription)
// ERROR OCCURS HERE
}
//let jsonResult: Any! = JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
// print(jsonResult)
} else {
print("error")
}
}) task.resume()
这是我的FileZilla目录的屏幕截图:
(我刚刚意识到图片实际上没有在Payments目录中显示文件'token.php',但它就在那里。)
正如您所看到的,我正确地进入正确的目录以访问该文件,并执行正确的方法来完成HTTP请求。然而,奇怪的是它会在'catch'之后总是导致错误,但是当我尝试打印错误的本地化描述时它将显示nil。这是token.php
中的php代码<?php
require_once('stripe-php/init.php');
\Stripe\Stripe::setApiKey("sk_test_eRFc0VoIi7mo7zBuArrznB5a");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$amount = $_POST['amount'];
$type = $_POST['type'];
$name = $_POST['name'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount,
"currency" => "usd",
"source" => $token,)
);
//email the purcahse and code info to your email
mail("willcohen2000@gmail.com","Purchase done","amount: $amount , type: $type, name: $name");
// create a json output with completion variable, (this will be read from the ios app as response)
$json = array(
'completion' => 'done'
);
echo json_encode($json);
} catch(\Stripe\Error\Card $e) {
// The card has been declined
$errorMessage = $e->getMessage();
$json = array(
'completion' => $getMessage()
);
echo json_encode($json);
}
?>
由于可能有点明显,我对后端工作相对缺乏经验,所以任何解释或指向正确的方向都将受到高度赞赏。感谢。