stripe / stripe-ios

Stripe iOS SDK
https://stripe.com
MIT License
2.09k stars 975 forks source link

No valid API key provided #1871

Closed Annate20105 closed 3 years ago

Annate20105 commented 3 years ago

Summary

After configurations settings in frontend and backend. Getting log : **["ui_usage_level": "none", "bindings_version": "21.8.1", "source_type": "card", "apple_merchant_identifier": "unknown", "app_version": "1.0.11", "additional_payment_methods": "none", "product_usage": [], "os_version": "14.5", "required_billing_address_fields": "zip", "ocr_type": "none", "publishable_key": "pk_test_51IYDGvI5yeI4F8g***Nrx", "additional_info": [], "device_type": "x86_64", "event": "stripeios.payment_intent_confirmation", "required_shipping_address_fields": "none", "analytics_ua": "analytics.stripeios-1.0", "company_name": "Cleaning Stage", "app_name": "Cleaning Stage", "shipping_type": "shipping", "apple_pay_enabled": 1]**

When I try to pay to get error: - some : Error Domain=com.stripe.lib Code=50 "There was an unexpected error -- try again in a few seconds" UserInfo={NSLocalizedDescription=There was an unexpected error -- try again in a few seconds, com.stripe.lib:StripeErrorTypeKey=invalid_request_error, com.stripe.lib:ErrorMessageKey=No valid API key provided. Set STPAPIClient.shared().publishableKey to your publishable key, which you can find here: https://stripe.com/docs/keys, com.stripe.lib:StripeErrorCodeKey=}

I tried to set STPAPIClient.shared.publishableKey in func stripeConfigure(), still getting the error.

Code to reproduce

private func stripeConfigure() {
    StripeAPI.defaultPublishableKey = AppKey.stripeAppKey
}
func billing(with cardParams:STPCardParams,
             and clientSecret:String,
             completition: @escaping (Bool, Error?) -> Void) {
    let detail = STPPaymentMethodBillingDetails()
    detail.name = cardParams.name
    let card = STPPaymentMethodCardParams(cardSourceParams: cardParams)
    let intent = STPPaymentIntentParams(clientSecret:clientSecret)
    intent.paymentMethodParams = STPPaymentMethodParams(card: card, billingDetails: detail, metadata: nil)
    STPAPIClient.shared.confirmPaymentIntent(with: intent) {intent, error in
        completition(intent?.status == .succeeded, error)
    }
}
func billing(with paymentMethodID:String,
             and clientSecret:String,
             completition: @escaping (Bool, Error?) -> Void) {
    let intent = STPPaymentIntentParams(clientSecret:clientSecret)
    intent.paymentMethodId = paymentMethodID
    STPAPIClient.shared.confirmPaymentIntent(with: intent) {intent, error in
        completition(intent?.status == .succeeded, error)
    }
}

iOS version

13 and over

Installation method

Cocoapods

SDK version

21.8.1

aliriaz-stripe commented 3 years ago

Hello! @Annate20105 , you should set the publishable key to your STPAPIClient singleton, so

STPAPIClient.shared.publishableKey = "pk_test_123"