Stripe redirectToCheckout句柄结果(Angular)



我有使用Stripe checkout 付款的代码

这是代码

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
});
});
}

现在它工作得很好,在成功之后,我转到SuccesUrl,它在会话中定义,在后端创建。

但我也需要获取函数的结果并运行另一个函数。我该怎么做?

更新

这是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_);
}));
}

转换redirectToCheckout函数以返回Observable,然后您就可以订阅它。

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
});
});
}

redirectToCheckout函数中将Promise转换为Observable

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

最新更新