在服务器端使用PHP App Stripe与PHP集成

时间:2016-08-10 20:51:40

标签: stripe-payments

我在iOS App上遇到Stripe Integration问题。

根据Stripe doc,

1)iOS应用程序需要首先生成令牌并将该令牌传递给自己服务器上的php lib

import UIKit
import Stripe

class ViewController: UIViewController, STPPaymentCardTextFieldDelegate {

    let paymentTextField = STPPaymentCardTextField()

    override func viewDidLoad() {
        super.viewDidLoad()

        Stripe.setDefaultPublishableKey("pk_test_xxxxxxxxxxxxxxxxxxxxx")

        // Do any additional setup after loading the view, typically from a nib.

        paymentTextField.frame = CGRectMake(15, 15, CGRectGetWidth(self.view.frame) - 30, 44)
        paymentTextField.delegate = self
        view.addSubview(paymentTextField)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func save(sender: AnyObject) {
        if let card:STPCardParams = paymentTextField.cardParams {
            STPAPIClient.sharedClient().createTokenWithCard(card) { (token, error) -> Void in
                if let error = error  {
                    print(error)
                } else if let token = token {
                        self.createBackendChargeWithToken(token) { status in
                    }
                }
            }
        }
    }

    func createBackendChargeWithToken(token: STPToken, completion: PKPaymentAuthorizationStatus -> ()) {
        let url = NSURL(string: "http://localhost:7777/myproject/index.php/rest/stripe/submit")!
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"

        let body = "stripeToken=\(token.tokenId)"

        print(body)

        request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
        let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
        let session = NSURLSession(configuration: configuration)

        print(request)
        let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
            if error != nil {
                completion(PKPaymentAuthorizationStatus.Failure)
                print("Fail to charge")
            }
            else {
                completion(PKPaymentAuthorizationStatus.Success)
                print("Successfully Charegd")
                print(PKPaymentAuthorizationStatus.Success)
            }
        }
        task.resume()
    }
}

2)php lib像Stripe API一样执行


\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxx");

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

$token = $_POST['stripeToken'];

$charge = \Stripe\Charge::create(array(
                    "amount" => 5000, // amount in cents, so need to multiply with 100 .. $amount * 100
                    "currency" => "usd",
                    "source" => $token,
                    "description" => "Test Order From iOS"
                )); 
}

3)工作正常,我可以在条纹仪表板上收到付款交易。

4)但是当我切换到另一个条带帐户(如客户帐户)时,我需要更改可发布密钥(来自移动应用程序)密钥(来自服务器端)< / b>对吗?

5)更改新密钥后,在条带仪表板上无法再接收付款。这是来自Stripe的日志消息。

{error: {
type: "invalid_request_error"
message: "Stripe no longer supports API requests made with TLS 1.0. Please initiate HTTPS connections with TLS 1.2 or later. You can learn more about this  at https://stripe.com/blog/upgrading-tls."}
}

错误状态为401

我的问题是,当来自另一个Stripe帐户的切换API(可发布和秘密)密钥时,应用程序无法正常工作?

1 个答案:

答案 0 :(得分:0)

错误对象提供的消息中给出了答案;您的PHP(或堆栈中的其他层)未配置为通过协议版本足够高的http S 发出网络请求(表示最低版本为1.2,该版本已经相当老-1.0年前被证明是易碎的)...这将是非常不安全的,因此当然是不允许的。

与Stripe帐户完全无关,但堆栈(LAMP)配置已更改。

,如果您实际上只是来回更改完全相同的文件上的文本,并且仅在一个帐户上得到此错误,则帐户的状态可能有所不同;我记得几周/几个月前,我不得不通过仪表板进行一次重大更新。

很可能一个帐户执行了此更新,而另一个帐户则没有执行此更新;因此使用了具有不同安全要求的不同API版本