ios - 在 Stripe iOS 实现中调用 Add Token 委托(delegate)后的过程是什么?

标签 ios swift stripe-payments

我正在使用 Stripe 和 iOS 向我的应用程序添加支付功能。

我知道我需要向我的服务器提交 token 和其他信息以完成该过程,但我不确定如何处理该函数以显示成功、关闭 Stripe Controller 并返回我的应用程序。

func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: @escaping STPErrorBlock) {
    self.submitTokenToBackend(token: token, completion: { (error: Error?) in
        if let error = error {
            completion(error)
        } else {
            self.dismiss(animated: true, completion: {
                //self.showReceiptPage()
                completion(nil)
            })
        }
    })
}

func submitTokenToBackend(token: STPToken, completion: (_ error:Error)->()){
    print("doing this")
}

我使用 Alamofire 作为我的传输引擎。

最佳答案

我也在服务器上使用带有 swift 和 asp.net web api 的 Stripe,我将把我使用的完整过程完美运行:

1) 服务器 - asp.net web api with library stripe:

    [Route("PostCharge")]
    [HttpPost]
    [ResponseType(typeof(Ride))]
    public async Task<IHttpActionResult> PostCharge(StripeChargeModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        var chargeId = await ProcessPayment(model);
        return Ok(chargeId);
    }

    private async Task<string> ProcessPayment(StripeChargeModel model)
    {
        return await Task.Run(() =>
        {
            var myCharge = new StripeChargeCreateOptions
            {
                Amount = (int)(model.Amount * 100),
                Currency = "usd",
                Description = model.CardHolderName + "Charge",
                StatementDescriptor = model.CardHolderName,
                SourceTokenOrExistingSourceId = model.Token
            };
            var chargeService = new StripeChargeService("sk_test_laskdjfasdfafasd");
            var stripeCharge = chargeService.Create(myCharge);
            return stripeCharge.Id;
        });
    }

2) 带 Alamofire 和 Stripe 库的 Swift 3:

        STPAPIClient.shared().createToken(withCard: card, completion: { (token, error) -> Void in
        if error != nil {
            self.hideProgress()
            self.showAlert(self, message: "Internet is not working")
            print(error)
            return
        }
        let params : [String : AnyObject] = ["Token": token!.tokenId as AnyObject, "Amount": paymentAmount as AnyObject, "CardHolderName": AppVars.RiderName as AnyObject]

        Alamofire.request(url + "/api/postcharge", method: .post, parameters: params, encoding: JSONEncoding.default, headers: [ "Authorization": "Bearer " + token]).responseJSON { response in
            switch response.result {
            case .failure(_):
                self.hideProgress()
                self.showAlert(self, message: "Internet is not working")
            case .success(_):
                let dataString:NSString = NSString(data: response.data!, encoding: String.Encoding.utf8.rawValue)!
                if (dataString as? String) != nil {
                   self.showAlert(self, message: "Your payment has been successful")
                } else {
                  self.showAlert(self, message: "Your payment has not been successful. Please, try again")
                }
            }
        }
    })

3)swift 3,在AppDelegate的应用中插入一行:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    STPPaymentConfiguration.shared().publishableKey = "pk_test_xxasdfasdfasdf"
    return true
}

关于ios - 在 Stripe iOS 实现中调用 Add Token 委托(delegate)后的过程是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40348483/

相关文章:

ios - 使用 UIGraphicsBeginImageContext 将 UICollectionViewCell 保存为图像

javascript - Stripe Connect 无需验证即可进行转账和付款

c# - 使用 .net Framework 4.7.2 而不是 .net core 实现 strip 订阅

ios - swift : equivalent to respondToSelector

php - 如何在 wordpress 中创建带有限制语句的 RSS 提要

ios - 取消 Apple 自动续订订阅

Swift 在递归使用时假定泛型类型

ios - 从 json 循环数组

ios - 执行 if let 语句会导致错误

objective-c - 几个不推荐使用的对象集成 Stripe API IOS 9