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

时间:2016-10-31 18:40:45

标签: ios swift stripe-payments

我正在使用Stripe和iOS向我的应用添加付款。

我知道我需要将令牌和其他信息提交给我的服务器来完成这个过程,但是我不知道如何处理显示成功的函数,关闭Stripe控制器,然后返回我的应用程序。 / p>

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作为我的传输引擎。

1 个答案:

答案 0 :(得分:1)

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

1)服务器 - 带有库条带的asp.net web api:

    [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)Swift 3与Alamofire和Stripe库:

        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
}