2016-04-23 55 views
0

我试图在iOS中从Alamofire发布一个令牌到PHP文件(https://thawing-inlet-46474.herokuapp.com/charge.php),但是Stripe不显示任何完成的费用。这是确切的PHP文件:从iOS发布一个Params请求到PHP Heroku服务器

<?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); 
} 

?> 

这是斯威夫特代码:

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后的反应是成功:后跟空格..任何人有一个想法是什么问题?

+0

1 /捕捉**所有**错误,而不仅仅是卡的错误。 2 /在仪表板中查看日志:https://dashboard.stripe.com/logs。 3 /在您的服务器上查看错误日志。 99%确定你在iOS应用程序中使用了错误的API密钥,这是最常见的错误。 – koopajah

+0

谢谢!除了更改参数类型之外,我实际上确实需要更改API密钥。 – Zach

回答

0

两件事情解决了这个问题:

改变我的PHP文件,以正确的密钥的API密钥,并从一个Integer调整“量”类型为String:

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)") 
      } 
    } 

} 
相关问题