2015-12-30 251 views
1

確定經過多次搜索後,我不知道這是怎麼回事,因爲我認爲我在更換密鑰時工作正常。然而,它不再工作,我什麼都沒有改變。不要報告重複,請不要回答任何問題。數據無法被讀取,因爲它的格式不正確

let URL = "http://localhost/donate/payment.php" 
    let params: Dictionary<String,AnyObject> = ["stripeToken": token.tokenId, 
     "amount": Int(self.amountTextField.text!)!, 
     "currency": "usd", 
     "description": self.emailTextField.text!] 



    let manager = AFHTTPSessionManager() 
    manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/plain", "text/html", "application/json"]) as! Set<String> 

    manager.PATCH(URL, parameters: params, success: { (operation, responseObject) -> Void in 
     guard let response = responseObject as? [String: String] else { 
      print("failed") 
      return 
     } 
     }) { (operation, error) -> Void in 

      self.handleError(error) 
      } 

這裏是我的PHP代碼,我從appcoda了(我從來沒有在PHP編碼)

<?php 
     require_once('vendor/autoload.php'); 
     \Stripe\Stripe::setApiKey("key"); 
     $token = $_POST['stripeToken']; 
     $amount = $_POST['amount']; 
     $currency = $_POST['currency']; 
     $description = $_POST['description']; 
      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 
    } 
    ?> 
+0

爲什麼你使用'PATCH'方法使用'POST'而不是 –

+0

已過時iOS 9的帖子 – mn1

+0

@HamzaAnsari我想這是問題所在。你想將它作爲答案發布,以便我可以選擇它作爲正確的答案。如果你還可以解釋爲什麼補丁不是很好的方法。謝謝 – mn1

回答

0

PATCH方法是使用,如果你要更新現有資源。

鑑於待處理

POST的提交數據(例如,從HTML表格)所標識的資源。數據包含在請求的正文中。 這可能會導致創建新資源或更新現有資源或兩者。

而在你的PHP文件您正在使用POST方法,讓你通過請求通過,而你正在使用PATCH方法傳遞的指標的影響參數和這就是爲什麼你的服務器是在respnonse返回錯誤的原因

+0

很感謝解釋。 – mn1

相關問題