Search code examples
androidkotlinstripe-paymentspaymentstrip

Getting status "Incomplete" on stripe always


I'm always getting incomplete at onCompletePayment and I'm also checked stripe sample app but it's also not working for me. I have check lot but I unable rectify the issue.

So what's would be error on my side?

Source :

PaymentConfiguration.init(BuildConfig.STRIPE_PUBLISHABLE_KEY)
    /* Initialized customer*/

 private fun setupPaymentSession() {
    mPaymentSession = PaymentSession(mvpView?.baseActivity!!)

    val paymentSessionInitialized = mPaymentSession!!.init(object : PaymentSession.PaymentSessionListener {


override fun onCommunicatingStateChanged(isCommunicating: Boolean) {
            if (isCommunicating) {
            } else {
            }
        }

        override fun onError(errorCode: Int, errorMessage: String?) {
        }

        override fun onPaymentSessionDataChanged(data: PaymentSessionData) {
            mPaymentSessionData = data
            checkForCustomerUpdates()
        }
    }, PaymentSessionConfig.Builder()
            /* .setPrepopulatedShippingInfo(getExampleShippingInfo())
             .setHiddenShippingInfoFields(ShippingInfoWidget.PHONE_FIELD, ShippingInfoWidget.CITY_FIELD)*/
            .setShippingInfoRequired(false)
            .build())
    if (paymentSessionInitialized) {
        mPaymentSession?.setCartTotal(20L)
    }
}

    override fun handlePaymentData(requestCode: Int, resultCode: Int, data: Intent?) {
    if (data != null) {
        mPaymentSession?.handlePaymentData(requestCode, resultCode, data)
        mPaymentSession?.completePayment(PaymentCompletionProvider { paymentData, listener ->
            Toast.makeText(mvpView?.baseActivity!!, "success" + paymentData.paymentResult, Toast.LENGTH_SHORT).show()
            listener.onPaymentResult(paymentData.paymentResult)
        })
    }
}

Solution

  • Not familiar with Kotlin but in the code snippet you provided, I'd suggest not to override handlePaymentData.

    Instead call mPaymentSession.handlePaymentData in the onActivityResult of your host Activity like it is suggested in the doc here or as shown in the example here so that any updates to the PaymentSessionData is reported to your PaymentSessionListener that you attached when initializing PaymentSession (i.e with mPaymentSession!!.init).

    Also generally, depending on your app Checkout flow, you would want to call mPaymentSession.completePayment(...) as a result of your user clicking for example on a "Pay" button.

    You would pass to the completePayment(...) call a PaymentCompletionProvider which would:

    1. send an HTTP request to your backend so that you can create a charge using Stripe's API
    2. mark the result of the payment using listener.onPaymentResult(...) passing PaymentResultListener.SUCCESS in the case where the payment was for example successful.

    I don't think that the example app has an example of this but in Java you could for example have a click listener on your "Pay" button setup like below:

    Button payButton = findViewById(R.id.pay_bttn);
    payButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            if (mPaymentSessionData.isPaymentReadyToCharge() && mPaymentSessionData.getPaymentResult() == PaymentResultListener.SUCCESS) {
                // Use the data to complete your charge - see below.
                mPaymentSession.completePayment(new PaymentCompletionProvider() {
                    @Override
                    public void completePayment(@NonNull PaymentSessionData data, @NonNull PaymentResultListener listener) {
                        Log.v(TAG, "Completing payment with data: " + data.toString());
    
                        // This is where you want to call your backend...
                        // In this case mark the payment as Successful
                        listener.onPaymentResult(PaymentResultListener.SUCCESS);
                    }
                });
            }
        }
    });
    

    I hope this helps.