简体   繁体   English

如何从支付网站重定向回我的 ionic 应用程序

[英]how to redirect back to my ionic app from a payment site

Good everyone.大家好。 Please I need your assistance.请我需要你的帮助。 I am using a payment gateway that needs to redirect back to my ionic app after a successful transaction.我使用的支付网关需要在交易成功后重定向回我的 ionic 应用程序。 But after a successful transaction, I am having this error message "can't post to localhost:8100/home" but when I use a site URL for example( https://siteurl.com ), it works by redirecting to the specified site.但是在成功交易后,我收到此错误消息“无法发布到 localhost:8100/home”,但是当我使用站点 URL 时(例如https://siteurl.Z4D236D9A2A2D102C5FE6AD1 ),它通过 504B391A8D2C5FE6AD1 重定向到指定的地址)地点。 I don't know where I am getting it wrong and what will be the callback URL to my app.我不知道我在哪里弄错了,我的应用程序的回调 URL 将是什么。 Here is my code.这是我的代码。 Thanks.谢谢。

completePurchase() {
    this.loadingCtrl.create({
      message: "Order Processing...",
      showBackdrop: true
    }).then((overlay) => {
      this.loading = overlay;
      this.loading.present();
      let currentCustomerId = localStorage.getItem('currentUserId');
      if (this.paymentGatwayId == "tbz_rave") {
        this.rave.init(false, "PUBLIC_KEY") //true = production, false = test
          .then(_ => {
            var paymentObject = this.ravePayment.create({
              customer_email: this.user.email,
              amount: this.totalPrice,
              customer_firstname: `${this.user.first_name}`,
              customer_lastname: `${this.user.last_name}`,
              customer_phone: `${this.user.billing.phone}`,
              currency: "NGN",
              txref: "rave-1234550",
              redirect_url: "http://localhost:8100/home",
              meta: [{
                metaname: "flightID",
                metavalue: "AP1234"

              }]
            })
            this.rave.preRender(paymentObject)
              .then(secure_link => {
                secure_link = secure_link + " ";
                const browser: InAppBrowserObject = this.rave.render(secure_link, this.iab);
                browser.on("loadstart")
                  .subscribe((event: InAppBrowserEvent) => {
                    if (event.url.indexOf('http://localhost:8100/home') != -1) {

                      if (event.url.includes("payment_successfull")) {

                        browser.close();

                        console.log("Transaction Succesful");

                        // Place order after payment successfull
                          let orderObj = {};
                    orderObj['payment_method'] = this.paymentGatwayId;
                    orderObj['payment_method_title'] = this.paymentGatewayTitle;
                    orderObj['customer_id'] = currentCustomerId;
                    this.platform.ready().then(() => {
                      this.address = {
                        first_name: this.user.first_name,
                        last_name: this.user.last_name,
                        address_1: this.user.billing.address_1,
                        city: this.user.billing.city,
                        address_2: this.user.billing.address_2,
                        phone: this.user.billing.phone,
                      }
                      orderObj['billing'] = this.address;
                      orderObj['line_items'] = this.baseProducts;
                      this.WC.placeOrder(orderObj).then(async (respData) => {
                        this.storage.clear();
                        this.storage.set('currentOrderData', respData);
                        console.log(orderObj);
                        //navigate after successful placing of other
                        const alert = await this.alertCtrl.create({
                          cssClass: 'my-custom-class',
                          header: 'Status',
                          message: ' <strong>Transaction successful</strong>!',
                          buttons: [
                            {
                              text: 'Okay',
                              handler: () => {
                                this.route.navigate(['/menu/order']);
                              }
                            }
                          ]
                        });

                        await alert.present()

                        this.route.navigate(['/menu/order']);
                      }).catch((error) => {
                        console.log('Problem with placing order', error);
                      });
                    });

                      } else {
                        browser.close();
                        console.log("Transaction fail");

                      }
                      browser.close()
                    }
                  })
              }).catch(error => {
                // Error or invalid paymentObject passed in
                console.log("error", error);
              })
          });
      }
    });
    setTimeout(() => {

      this.loading.dismiss();
    }, 5000);
  }

Service page for placing an order after a successful transaction交易成功后下单服务页面

placeOrder(orderDataObj){
  let headers = new HttpHeaders ({
    'Content-Type': 'application/x-www-form-urlencoded'
  });

  let orderData = this.JSON_to_URLEncoded(orderDataObj);

  this.apiUrl = `${this.siteUrl}${this.woocommercePath}orders?consumer_key=${this.consumerKey}&consumer_secret=${this.consumerSecret}`;
  console.log('API URL for order: ', this.apiUrl);

  return new Promise ((resolve) => {
    this.orderResp = this.http.post(this.apiUrl,orderData, {headers});
    this.orderResp.subscribe((responseData) => {
      resolve(responseData);
    });
  });

}

Few days back even i"m facing the same issue, technically when the ionic apk file running on the mobile/smartphone doesn't have any port number running on ie, http://localhost/dashboard instead of http://localhost:8100/dashboard.几天前,即使我面临同样的问题,从技术上讲,当移动/智能手机上运行的 ionic apk 文件没有运行任何端口号时,即http://localhost/dashboard而不是 http://localhost: 8100/仪表板。

So specify the redirect url as redirect_url: "http://localhost/home"因此将重定向 url 指定为redirect_url: "http://localhost/home"

or to land back to same window/page where the payment started specify as redirect_url:window.location.href,或返回到付款开始的同一窗口/页面,指定为redirect_url:window.location.href,

then you will be able to redirect back to the ionic app.然后您将能够重定向回离子应用程序。

I think that you are doing it wrong;我认为你做错了; when the transaction succeeds the payment websites will notfiy you on a webhook URL this url should be on your server because they are a post requests.当交易成功时,支付网站将通过 webhook URL 通知您,此 url 应该在您的服务器上,因为它们是一个发布请求。 You cannot send a post request directly to your client (Browser/IONIC client) because they are not supposed to act as a web server.您不能直接向您的客户端(浏览器/IONIC 客户端)发送发布请求,因为它们不应该充当 web 服务器。 A simple solution would be to create an URL on your server that that receives the response and then redirect the user to your web app (Not sure if it is working with IONIC) in No:一个简单的解决方案是在您的服务器上创建一个接收响应的 URL,然后将用户重定向到您的 web 应用程序(不确定它是否与 IONIC 一起使用)在否:

http.post('/rave-callback',function(req,res){ 
// you can store the payment details in the db if you need to
   res.redirect('http://localhost:8100/home')
})

In your IONIC app you must change the redirect_url to redirect_url: "http://my-server-url.com/rave-callback在您的 IONIC 应用程序中,您必须将 redirect_url 更改为redirect_url: "http://my-server-url.com/rave-callback

more infos about this here: https://medium.com/@jake_parkers/3d-secure-guidelines-9e17f9a6cf32更多关于这里的信息: https://medium.com/@jake_parkers/3d-secure-guidelines-9e17f9a6cf32

We don't have the full story so I mainly gussing, so I guess this is your problem:我们没有完整的故事,所以我主要是猜测,所以我想这是你的问题:

if (event.url.indexOf('http://localhost:8100/home') != -1) {
  if (event.url.includes("payment_successfull")) {
  }
}

How do you know the payment gateway will redirect to http://localhost:8100/home ?你怎么知道支付网关会重定向到http://localhost:8100/home It will not work becuase this is just YOUR local serv.它不起作用,因为这只是您的本地服务。 If you configured it in the payment gateway provider you need to change that.如果您在支付网关提供商中配置它,则需要更改它。

Your best bet (what I did) is to leave it at whatever defulat success page it already has, this page should have some keyword that you can use this function to check event.url.includes("your keyword here")你最好的选择(我所做的)是将它留在它已经拥有的任何 defulat 成功页面上,这个页面应该有一些关键字,你可以使用这个 function 来检查event.url.includes("your keyword here")

Or if you have to fill-in a value use their page and just add a parameter at the end with your keyword: www.url.com?status=payment_successfull或者,如果您必须填写一个值,请使用他们的页面并在末尾添加一个带有关键字的参数: www.url.com?status=payment_successfull

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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