2016-04-23 73 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)") 
      } 
    } 

}