如何使用cordova / phonegap在inApp Browser中打开Paypal

时间:2016-12-14 06:34:58

标签: javascript android cordova phonegap-cli

我正在使用PhoneGap中的JavaScript集成PayPal集成,但是我遇到的问题是它没有在inApp浏览器中打开。而不是它在我的应用程序之外打开。所以我希望在inApp浏览器中打开PayPal URL 我是PhoneGap的新手,所以如果有人知道请帮忙。以下是代码

<form id="openpaypal" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="NSH74TD463ZSU">
<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer,  easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

//script here

<script>
        document.getElementById("openpaypal").addEventListener("click", openBrowser);

        function openBrowser() {
            alert("called here");
            var url = 'https://www.paypal.com/cgi-bin/webscr';
            var target = '_blank';
            var options = "location=yes"
            var ref = cordova.InAppBrowser.open(url, target, options);

            ref.addEventListener('loadstart', loadstartCallback);
            ref.addEventListener('loadstop', loadstopCallback);
            ref.addEventListener('loadloaderror', loaderrorCallback);
            ref.addEventListener('exit', exitCallback);

            function loadstartCallback(event) {
                console.log('Loading started: ' + event.url)
            }

            function loadstopCallback(event) {
                console.log('Loading finished: ' + event.url)
            }

            function loaderrorCallback(error) {
                console.log('Loading error: ' + error.message)
            }

            function exitCallback() {
                console.log('Browser is closed...')
            }
        }
    </script>

1 个答案:

答案 0 :(得分:1)

对于cordova命令

首先添加插件

Here是链接

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git

这是你的脚本

<script src="assets/js/cordova.js" type="text/javascript"></script> 
<script src="assets/js/min/app.js" type="text/javascript"></script>

但是,在构建应用cordova.js时,会自动添加到www文件夹的根路径

所以添加如下

<script src="cordova.js" type="text/javascript"></script> 

对于PhoneGap Build

将此行添加到config.xml文件

<plugin name="org.li8.inappbrowser" spec="0.1" source="pgb" />

设置脚本

<script src="phonegap.js" type="text/javascript"></script> 

被修改

在设备准备就绪后调用您的功能可能会给您带来问题

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady()
{
    document.getElementById("openpaypal").addEventListener("click", openBrowser);
}

function openBrowser() 
{
    var url = 'https://www.paypal.com/cgi-bin/webscr';
    var target = '_blank';
    var options = "location=yes";
    var ref = window.open(url, target, options);
}