Android Stripe app crashes on adding new payment source
Asked Answered
M

3

7

Desired payment flow

Am trying to achieve the above checkout flow using stripe SDK in android using this documentation https://stripe.com/docs/mobile/android/customer-information. I have created a backend call that returns the ephemeral key like this

{
    "id": "ephkey_EPHEMERAL_KEY_HERE",
    "object": "ephemeral_key",
    "associated_objects": [
        {
            "id": "cus_CUSTOMER_ID_HERE",
            "type": "customer"
        }
    ],
    "created": 1535352558,
    "expires": 1535356158,
    "livemode": true,
    "secret": "ek_live_SECRET_HERE"
}

In my app, am initializing my CustomerSession and starting the PaymentMethodsActivity like this

CustomerSession.initCustomerSession(
                new MyEphemeralKeyProvider(
                        new MyEphemeralKeyProvider.ProgressListener() {
                            @Override
                            public void onStringResponse(String string) {

                                if (string.startsWith("Error: ")) {
                                    new android.support.v7.app.AlertDialog.Builder(SelectCardActivity.this).setMessage(string).show();
                                }

                                Intent payIntent = PaymentMethodsActivity.newIntent(SelectCardActivity.this);
                                startActivityForResult(payIntent, REQUEST_CODE_SELECT_SOURCE);
                            }
                        }));

Then i have this onActivityResult method

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SELECT_SOURCE && resultCode == RESULT_OK) {
            String selectedSource = data.getStringExtra(PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT);
            Source source = Source.fromString(selectedSource);
            // This is the customer-selected source.
            // Note: it isn't possible for a null or non-card source to be returned at this time.
        }
    }

It opens the PaymentMethodsActivity fine and on adding a new payment method, when i try to submit the app crashes with the following stack trace. anything i might have missed?

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.stealthdroids.itransfer, PID: 22404
                  java.lang.IllegalStateException: Attempted to get instance of PaymentConfiguration without initialization.
                      at com.stripe.android.PaymentConfiguration.getInstance(PaymentConfiguration.java:29)
                      at com.stripe.android.view.AddSourceActivity.onActionSave(AddSourceActivity.java:133)
                      at com.stripe.android.view.StripeActivity.onOptionsItemSelected(StripeActivity.java:88)
                      at com.stripe.android.view.AddSourceActivity.onOptionsItemSelected(AddSourceActivity.java:33)
                      at android.app.Activity.onMenuItemSelected(Activity.java:2970)
                      at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
                      at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                      at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                      at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                      at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63)
                      at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203)
                      at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
                      at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                      at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
                      at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
                      at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
                      at android.view.View.performClick(View.java:4848)
                      at android.view.View$PerformClick.run(View.java:20270)
                      at android.os.Handler.handleCallback(Handler.java:815)
                      at android.os.Handler.dispatchMessage(Handler.java:104)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5668)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
Mint answered 27/8, 2018 at 11:14 Comment(0)
J
15

It's complaining that you've not initialized the PaymentConfiguration object with a publishable key.

PaymentConfiguration.init(<<YOUR PUBLISHABLE KEY HERE>>);
Jacklin answered 27/8, 2018 at 15:33 Comment(2)
wow, thank you so much man, i almost killed myself trying to figure this outMint
@Jacklin from where i can get this publishable key?Felice
L
2

It should be like this

PaymentConfiguration.init(applcationContext, "publishableKey");
Lubbock answered 24/9, 2019 at 9:55 Comment(0)
F
0

in order to fix this error you need to add below line of code before your customer session initialization

    `PaymentConfiguration.init(this,"<past your publishable key here>");`

Now you can get your publishable key from this link https://dashboard.stripe.com/test/apikeys after login to the account.

Felice answered 1/2, 2021 at 3:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.