Paypal付款:如何在webview中加载paypal时获得成功请求

时间:2016-06-01 07:13:56

标签: android paypal paypal-sandbox

修改

在paypal登录后,我可以成功完成交易。但是我需要在paypal中匹配 successUrl 来验证两个url是否相同,然后显示成功的toast消息。

但我没有从付款中获得成功。所以我无法与之匹敌。我已经发布了相关代码:

WebActivity.java:

public class PaypalWebActivity extends Activity {

    private WebView webView;

    String payUrlStr;

    ProgressDialog dialog;

    String successUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.paypal_web_layout);

        successUrl = LOAD_WEBVIEW_PAYMENT_PAYPAL_SUCCESS;

        dialog = ProgressDialog.show(PaypalWebActivity.this, "", "Please wait..", false);

        loadWebViewPaypal();

    }

    private void loadWebViewPaypal() {

        payUrlStr = LOAD_WEBVIEW_PAYMENT_PAYPAL(PAGE_ID);       

        Log.e("payUrlStr", ""+payUrlStr);

        webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl(payUrlStr);
        webView.getSettings().setJavaScriptEnabled(true);

        @SuppressWarnings("unused")
        WebSettings settings= webView.getSettings();
        if (Build.VERSION.SDK_INT >= 21) {
            webView.getSettings().setMixedContentMode( WebSettings.MIXED_CONTENT_ALWAYS_ALLOW );
           }

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("Loading url...", url);
                view.loadUrl(url);

               String loadWebUrl = view.getUrl();

                Log.e("loadWebUrl", ""+loadWebUrl);

                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.e("Finished url...", url);

                String webUrl = view.getUrl();

                Log.e("webUrl", ""+webUrl);


                if(webUrl.substring(0,95).equals(successUrl)){

                    Log.e("Getting Success Request", "Test");

                }else{

                    Log.e("Failed to get Request", "Test");

                }

                if(dialog.isShowing()){
                    dialog.dismiss();
                }

            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {

                Log.e("Error in url...", description);
                Log.e("Error in failingUrl...", failingUrl);

            }

        });        

    }



}

清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

检查这个discussion。我会逐步添加截图并添加内容以便清楚理解。

我收到此错误消息。因此无法匹配成功请求:

  

06-15 18:12:59.894:I / chromium(3273):[INFO:CONSOLE(0)]“混合   内容:页面在   'https://www.sandbox.paypal.com/us/cgi-bin/webscr?SESSION=LTy9Q59%5fia3wiAdHTQjgQxvUF1BTzLjgXgelCew4AS%2dGAutAfB5WjZXVuX8&dispatch=5885d80a13c0db1f8e263663d3faee8dcce3e160f5b9538489e17951d2c62172'   是通过安全连接加载的,但包含一个目标表单   一个不安全的终点   “http://www.myapi-entertainment.com/page_managements/page_featured_subscription_payment_success/4。此内容也应通过HTTPS提交。   https://www.sandbox.paypal.com/us/cgi-bin/webscr?SESSION=LTy9Q59%5fia3wiAdHTQjgQxvUF1BTzLjgXgelCew4AS%2dGAutAfB5WjZXVuX8&dispatch=5885d80a13c0db1f8e263663d3faee8dcce3e160f5b9538489e17951d2c62172   (0)

这是我的成功回应“http://www.myapi-entertainment.com/page_managements/page_featured_subscription_payment_success/4” - &gt;更改了服务器名称。

是否可以将成功请求与paypal匹配?如果我有任何建议,对我有帮助。

5 个答案:

答案 0 :(得分:7)

Paypal在成功付款后返回json回复,如下所示

{
  "client": {
    "environment": "sandbox",
    "paypal_sdk_version": "2.14.1",
    "platform": "Android",
    "product_name": "PayPal-Android-SDK"
  },
  "response": {
    "create_time": "2016-06-15T11:38:04Z",
    "id": "PAY-6CN54299U76194116K5QT4BY",
    "intent": "sale",
    "state": "approved"
  },
  "response_type": "payment"
}

您需要检查响应json对象。如果州获得批准,则意味着PayPal付款成功。然后,您可以在Paypal网站上查看当前的交易。

确保您已按照以下步骤操作: 1) private static final int REQUEST_CODE_PAYMENT = 1; private static String CONFIG_ENVIRONMENT=PayPalConfiguration.ENVIRONMENT_SANDBOX; //实时模式的PayPalConfiguration.ENVIRONMENT_PRODUCTION。 2)

// note that these credentials will differ between live & sandbox environments.
private static final String CONFIG_CLIENT_ID = "ATBvU5urlaPOhpCrAhFsoG4u63RvNoKUocFPs9yR5q_sbM0yecZawUjoJhIilW8DNg5RrJcRHgRuEP_1";

private static PayPalConfiguration config = new PayPalConfiguration()
                                    .environment(CONFIG_ENVIRONMENT)
                                    .clientId(CONFIG_CLIENT_ID)
                                            // The following are only used in PayPalFuturePaymentActivity.
                                    .merchantName("Example Merchant")
                                    .merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
                                    .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));
                            Intent intent = new Intent(getActivity(), PayPalService.class);
                            intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
                            getActivity().startService(intent);

答案 1 :(得分:5)

我对您的查询了解的是,您有一个使用Paypal执行付款的网站网址,而在移动应用中,您使用Webview(而不是Paypal SDK)执行相同的操作。

,您可以通过在WEB端进行一些编码来获取callback

<强>含义:

每当任何用户导航到付款页面时,您的服务器必须知道用户是从网站访问还是通过移动Webview访问,服务器可以将additional parameter key发送到任何支付网关以获取其自定义逻辑。付款交易完成后,付款网关将返回相同的additional parameter key以及结果(成功或失败)。

注意:每个支付网关的设置均为Redirection url for Success/Failure

在交易完成后,一旦Paypal重定向到结果网址(成功/失败),然后服务器再次检查该请求是在additional parameter key的帮助下是从网站还是从移动网络视图发出的;检查下面的案例...

如果来自Mobile Webview

  • 成功:重定向到网址www.myserver.com/success
  • 失败:重定向到网址www.myserver.com/failure

如果来自Webiew,则Normal flow

现在在您的移动设备的Webview中

webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
                if(url.equalsIgnoreCase("www.myserver.com/success"))
                  //Success Toast
                else if(url.equalsIgnoreCase("www.myserver.com/failure"))
                   //Failure Toast
                return true;
            }

});

那就是它。

答案 2 :(得分:4)

修改

从屏幕截图中可以看出,您正在将订阅页面嵌入到您的网页视图中,并尝试将successUrl与返回网址匹配。

  1. 检查答案in this post,先在个人资料中设置回溯网址

  2. 如果您想使用定义的String webUrl = webView.getUrl();

  3. 进行匹配/验证,请获取网址successUrl

答案 3 :(得分:1)

如上所述,

你需要检查响应json对象。如果州获得批准意味着PayPal支付成功。那么您可以在Paypal网站上查看当前交易

答案 4 :(得分:1)

我正在使用以下代码获得付款成功或失败请求:

private void loadWebViewPaypal() {

        payUrlStr = LOAD_WEBVIEW_PAYMENT_PAYPAL(PAGE_ID);       

        Log.e("payUrlStr", "" + payUrlStr);

        webView = (WebView) findViewById(R.id.webView);

        webView.setWebViewClient(new WebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(payUrlStr);


        @SuppressWarnings("unused")
        WebSettings settings= webView.getSettings();

        if (Build.VERSION.SDK_INT >= 21) {
            webView.getSettings().setMixedContentMode( WebSettings.MIXED_CONTENT_ALWAYS_ALLOW );
           }


    }

    public class WebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            Log.e("Page Started", ""+url);

            super.onPageStarted(view, url, favicon);



            if(url.contains(successUrl)) {

                Log.e("Getting Success Request", "Test");

                Intent i = new Intent(PaypalWebActivity.this, TabhostActivity.class);

                PAYPAL_WEB_BACK = "fulfilled";
                startActivity(i);
                finish();

            } else if(url.equalsIgnoreCase(failureUrl)) {

                Intent i = new Intent(PaypalWebActivity.this, TabhostActivity.class);

                PAYPAL_WEB_BACK = "fulfilled";
                startActivity(i);
                finish();

            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.e("Override Url", ""+url);

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            Log.e("Finished Url :", "" + url);

            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
    }