【问题标题】:Posting a Params Request to PHP Heroku Server From iOS从 iOS 向 PHP Heroku 服务器发布参数请求
【发布时间】: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


【解决方案1】:

两件事解决了这个问题:

将我的 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)")
            }
    }

}

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多