简体   繁体   中英

Create managed stripe account with php and swift

so I'm trying to create managed stripe accounts with PHP Swift and Alamofire. But it's not working at all.

Here's my PHP code:

  <?php

require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("My APIKEY");

$country = $_POST['country'];

$create = \Stripe\Account::create(array(
 "country" => $country,
 "managed" => true
   )
);

?>

Here's my swift code:

 @IBAction func createBtn(_ sender: Any) {

        let card = STPCardParams()
        card.number = "5200828282828210"
        card.expMonth = 4
        card.expYear = 2024
        card.cvc = "242"
        card.currency = "usd"

        STPAPIClient.shared().createToken(withCard: card ,completion: {(token, error) -> Void in
            if let error = error {
                print("ERROR: \(error.localizedDescription)")
            }
            else if let token = token {
                print(token)
                self.createUsingToken(token:token)
            }
        })
    }

    func createUsingToken(token:STPToken) {
        let requestString = "My request URL"
          let params = ["token": token.tokenId, "country": "US"]

        //This line of code will suffice, but we want a response
        Alamofire.request(requestString, method: .post, parameters: params).responseJSON { (response) in
            print("REQUEST: \(response.request!)") // original URL request
            print("RESPONSE: \(response.response!)") // URL response
            print("DATA: \(response.data!)") // server data
            print("RESULT: \(response.result)") // result of response serialization
            if let JSON = response.result.error {
                print("JSON: \(JSON.localizedDescription)")
            }
        }
    }

And I'm getting this error from Alamofire: JSON: Response could not be serialized, input data was nil or zero length. Thanks for your help.

It looks like your Swift/Alamofire request is expecting a JSON response, but your PHP code is not sending any response at all: you're sending an account creation request to Stripe but then never outputting any data.

You likely want to prepare an array with the attributes that you expect in your Swift code, then output it as JSON at the end of your PHP script:

echo json_encode($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM