PayPal支付:如何在webview中加载PayPal时获得成功请求

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (166)

编辑:

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

我没有从报酬中获得成功,所以我无法与之相匹配。下面发布了相关代码:

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" />

无法与成功请求相匹配

提问于
用户回答回答于

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

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

一旦Paypal在事务完成后重定向到结果url(成功/失败),服务器将再次检查请求是来自网站还是来自移动WebViewadditional parameter key检查下面的情况..。

如果来自移动WebView

  • 成功:重定向到URLwww.myserver.com/success
  • 失败:重定向到URLwww.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;
            }

});

用户回答回答于

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对象。如果状态被批准,这意味着贝宝支付是成功的。然后,你可以检查贝宝网站的当前交易。

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

扫码关注云+社区