简体   繁体   中英

Stripe redirectToCheckout handle result (Angular)

I have code to make payment using Stripe checkout

Here is code

async pay(price: number, name: string, description: string) {
    const stripe = await loadStripe('***************');
    this._subscriptionService.createStripeSession(price, name, description, this.document.location.origin)
    .subscribe(async result => {
        this.sessionId = result;
        const { error } = await stripe.redirectToCheckout({
            sessionId: this.sessionId
        });
    });
}

Now it works well and after the success I go to SuccesUrl, that defined in session, that created on back end.

But also I need to catch the result of function and run another function. How I can do this?

UPDATE

Here is createStripeSession

createStripeSession(price: number | null | undefined, name: string | null | undefined, description: string | null | undefined, locationUrl: string | null | undefined): Observable<string> {
    let url_ = this.baseUrl + "/api/services/app/Subscription/CreateStripeSession?";
    if (price !== undefined)
        url_ += "price=" + encodeURIComponent("" + price) + "&"; 
    if (name !== undefined)
        url_ += "name=" + encodeURIComponent("" + name) + "&"; 
    if (description !== undefined)
        url_ += "description=" + encodeURIComponent("" + description) + "&"; 
    if (locationUrl !== undefined)
        url_ += "locationUrl=" + encodeURIComponent("" + locationUrl) + "&"; 
    url_ = url_.replace(/[?&]$/, "");

    let options_ : any = {
        observe: "response",
        responseType: "blob",
        headers: new HttpHeaders({
            "Accept": "application/json"
        })
    };

    return this.http.request("post", url_, options_).pipe(_observableMergeMap((response_ : any) => {
        return this.processCreateStripeSession(response_);
    })).pipe(_observableCatch((response_: any) => {
        if (response_ instanceof HttpResponseBase) {
            try {
                return this.processCreateStripeSession(<any>response_);
            } catch (e) {
                return <Observable<string>><any>_observableThrow(e);
            }
        } else
            return <Observable<string>><any>_observableThrow(response_);
    }));
}

Convert your redirectToCheckout function to return an Observable and then you can subscribe to it.

    async pay(price: number, name: string, description: string) {
        const stripe = await loadStripe('***************');
        this._subscriptionService.createStripeSession(price, name, description, this.document.location.origin)
        .subscribe(async result => {
            this.sessionId = result;
            const { error } = await stripe.redirectToCheckout({
                sessionId: this.sessionId
            }).subscribe(result2 =>{
               //run another function code here
            });
        });
    }

Converting Promise to Observable in your redirectToCheckout function

    import { from } from 'rxjs';

    redirectToCheckout(){
      return from(your promise code in here);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM