triniwiz / nativescript-plugins

Apache License 2.0
80 stars 50 forks source link

[IOS] requestPayment does not throw error if occure #134

Closed kefahB closed 1 year ago

kefahB commented 2 years ago

Hi @triniwiz

In IOS standard implementation, requestPayment will return always success (via the listener) even if createPaymentSession dont return a payment intent at all!

I've tried to reject a promise into capturePayment method and it return always success.

kefahB commented 1 year ago

Hi @triniwiz

I did have a little bit time to investigate this issu .. the error come from here, the Completion handler is set to null instead of handling error.

I've tested and it work

StripePaymentDelegate.prototype.paymentContextDidCreatePaymentResultCompletion = function (paymentContext, paymentResult, completion) {
        StripeStandardConfig.shared.backendAPI
            .capturePayment(paymentResult.paymentMethod.stripeId, paymentContext.paymentAmount, createShippingMethod(paymentContext), createAddress(paymentContext.shippingAddress))
            .then(function (value) {
            completion(STPPaymentStatus.Success, null);
        })
            .catch(function (e) {
            // this is the error
            // completion(null createError('PaymentError', 100, e)); 
            completion(STPPaymentStatus.Error, createError('PaymentError', 100, e));
        });
    };
kefahB commented 1 year ago

According to the Stripe docs we should handle 3 type of responses :

For instance only the success is handled by this plugin, I suggest to update this part for IOS as below if you agree ?

PS: I tested all 3 cases and it work as expected

StripePaymentDelegate.prototype.paymentContextDidCreatePaymentResultCompletion = function (paymentContext, paymentResult, completion) {
        StripeStandardConfig.shared.backendAPI
            .capturePayment(paymentResult.paymentMethod.stripeId, paymentContext.paymentAmount, createShippingMethod(paymentContext), createAddress(paymentContext.shippingAddress))
            .then(function (value) {
                if(!value._native.lastPaymentError || value._native.lastPaymentError == "undefined") {
                    completion(STPPaymentStatus.Success, null);
                    return
                }
                completion(STPPaymentStatus.UserCancellation, null);
            })
            .catch(function (e) {
            console.error("ERROR >> : paymentContextDidCreatePaymentResultCompletion : ", e);
            completion(STPPaymentStatus.Error, createError('PaymentError', 100, e));
        });
    };
kefahB commented 1 year ago

fixed with #141