直接从json文件传递上传的图像

时间:2017-01-10 17:55:42

标签: swift swift3 stripe-payments

我正在尝试将图片从我的应用上传到我的heroku后端,然后将其传递给Stripe进行验证。这是我上传和传递图像的快速代码:

@IBAction func registerAccountButtonWasPressed(sender: UIButton) {
    let dob = self.dobTextField.text!.components(separatedBy: "/")
    let URL = "https://splitterstripeservertest.herokuapp.com/account/create"
    var imageData = UIImageJPEGRepresentation(photoID,1)
    let params = ["year": UInt(dob[2])! as UInt] as [String : Any]

    let manager = AFHTTPSessionManager()
    let operation = manager.post(URL, parameters: params, constructingBodyWith: { (formData: AFMultipartFormData!) -> Void in
        formData.appendPart(withFileData: imageData!, name: "file", fileName: "photoID.jpg", mimeType: "image/jpeg")
    }, success: { (operation, responseObject) -> Void in
        print(responseObject!)
    }) { (operation, error) -> Void in
        self.handleError(error as NSError)
    }
}

我删除了上面的参数列表,并留下了一个可读性。

有没有办法接收这个文件并将其上传到条带而不必通过传递文件参数来保存它?像这样:

Stripe::FileUpload.create(
  {
    :purpose => 'identity_document',
    :file => params[file]
  },
  {:stripe_account => params[stripe_account]}
)

同样在条纹文档中,它说要将文件上传到“https://uploads.stripe.com/v1/filesbut then shows code to put in your backendStripe::FileUpload.create是否会为我上传条带进行上传?

对任何一方的任何见解都将非常感谢。

2 个答案:

答案 0 :(得分:3)

您需要先使用"create file upload"调用将文件上传到Stripe的API。然后,您可以使用文件上传的ID(file_...)到update the accountlegal_entity.verification.document属性。

(这个过程在这里解释:

由于该文件由用户提供,因此您有两种选择来创建文件上载:

  • 让您的应用将文件上传到后端服务器,然后在后端,将文件用于create the file upload

  • 直接从应用创建文件上传(使用可发布的API密钥),并将生成的file_upload的ID(file_...)发送到您的后端

以下是使用jQuery创建文件上传客户端的示例:https://jsfiddle.net/captainapollo/d8yd3761/

您可以在iOS应用的代码中执行相同的操作。基本上,您需要做的就是向https://uploads.stripe.com/v1/files发送一个POST请求,其Authorization标头的值为Bearer pk_...(其中pk_...是您可发布的API密钥)并输入{{1}将文件的内容作为请求的正文。此博文应该有助于使用Swift发送multipart/form-data个请求:http://www.kaleidosblog.com/how-to-upload-images-using-swift-2-send-multipart-post-request

答案 1 :(得分:0)

由于@Ywain,我能够为 IOS Swift 4 提出此解决方案。我直接从应用程序创建了文件上传,并检索了 file_upload的ID 发送到我的后端以附加到Connect Account。我通过导入Alamofire和SwiftyJSON做到了这一点。

let heads = ["Authorization": "Bearer \(stripePublishableKey)"]
let imageData = image!.jpegData(compressionQuality: 1.0)
let fileName = "\(NSUUID().uuidString).jpeg"


 Alamofire.upload(multipartFormData: { multipart in
            multipart.append(imageData!, withName: "file", fileName: fileName, mimeType: "image/jpeg")
            multipart.append(("identity_document").data(using: .utf8)!, withName :"purpose")

        }, to: fileUrl!, method: .post, headers: heads) { encodingResult in
            switch encodingResult {
              case .success(let upload, _, _):
                upload.responseJSON { answer in

                    let value = JSON(answer.value!)
                    let FileID = value["id"].stringValue


                    // Use fileID and Connect Account ID to create params to send to backend.

                    let params: [String: Any] = [
                        "acct_id": ConnectID!,
                        "fileID": FileID,
                        ]

                    print("statusCode: \(String(describing: answer.response?.statusCode))")
                }

            case .failure(let encodingError):
                print("multipart upload encodingError: \(encodingError)")
            }
        }