我创建了一个使用Stripe和PayPal的应用。为了获取令牌,我使用XAMPP在Mac上创建了一个本地后端服务器。
//。 。 。通过发出HTTP POST请求将令牌发送到我们的后端
func postStripeToken(token: STPToken) {
let URL = "http://localhost/ryde/payment.php"
let params = ["stripeToken": token.tokenId,
"amount": Double(self.driverInfoView.rydeFare.text!)!,
"currency": "cad",
"description": self.riderName] as [String : Any]
let manager = AFHTTPSessionManager()
manager.post(URL, parameters: params, success: { (operation, responseObject) -> Void in
if let response = responseObject as? [String: String] {
let alertController = UIAlertController(title: response["status"], message: response["message"], preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}) { (operation, error) -> Void in
self.handleError(error as NSError)
print(error)
}
}
payment.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_**********************");
// 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
}
?>
问题是,如果我使用本地服务器,我必须始终运行XAMPP,以便移动应用程序中没有打嗝。
如何创建外部后端而不是本地后端?
答案 0 :(得分:1)
您需要做的就是
1)启动在Ubuntu上运行的linux实例服务器。您可以使用AWS或heroku
2)通过命令行
在其上安装XAMPPwget http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/1.8.3/xampp-linux-x64-1.8.3-5-installer.run
3)使其可执行
sudo chmod +x xampp-linux-x64-1.8.3-5-installer.run
4)运行安装
sudo ./xampp-linux-x64-1.8.3-5-installer.run
5)启动Xampp。这将使Xampp启动并运行。
sudo /opt/lampp/lampp start
5)将PHP代码文件夹复制到/opt/lampp/htdocs
6)运行以下命令重新启动XAMPP
sudo /opt/lampp/lampp restart
7)最后将swift代码中的URL从localhost更改为远程服务器URL
let URL = "Your server URL/payment.php"
这就是你需要做的一切。