2

由于业务原因,我无法使用 paypal SDK(Express Checkout Library),所以我需要通过 webView 实现结帐。我需要知道如何传递 url(基于交易代码、价格、用户电子邮件等参数)以及如何在重定向页面后返回应用程序。

这是我的代码:

public class PayPalHandler extends Activity {
//The reference token that we get from initializing the MECL library
public static String _deviceReferenceToken;
private TextView mResultText;
private CheckoutButton mCheckoutButton;
private WebView _webView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super .onCreate(savedInstanceState);
    setContentView(R.layout.paypal);
    mResultText = (TextView) findViewById(R.id.tv_paypal);
    PayPalInitializer payPalInitializer = new PayPalInitializer(
            this );
    payPalInitializer.execute();
}

private class PayPalInitializer extends
        AsyncTask<Void, Void, Boolean> {

    private static final String APP_ID = "APP-80W284485P519543T";
    private Context mContext;
    private ProgressDialog mProgressDialog;

    public PayPalInitializer(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
        super .onPreExecute();
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog
                .setMessage("Loading...");
        mProgressDialog.show();

    }

    @Override
    protected Boolean doInBackground(Void... params) {
        boolean success = false;
        PayPal payPal = PayPal.getInstance();
        if (payPal == null) {
            payPal = PayPal.initWithAppID(mContext, APP_ID,PayPal.ENV_SANDBOX);
            payPal.setLanguage("en_US");

            if (payPal.isLibraryInitialized()) {
                success = true;
            }
        }
        return success;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super .onPostExecute(result);
        mProgressDialog.hide();
        mProgressDialog = null;
        if (result) {
            setupButton();
        } else {
            setupButton();
            //mResultText.setText("Done!");
        }
    }
};

private void setupButton() {
    TableRow row = (TableRow) findViewById(R.id.row_paypal);
    PayPal payPal = PayPal.getInstance();

    mCheckoutButton = payPal.getCheckoutButton(this , PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY);
    mCheckoutButton .setOnClickListener(mCheckoutButtonOnClickListener);
    row.addView(mCheckoutButton);
}

private static final int request = 1;
private View.OnClickListener mCheckoutButtonOnClickListener = new View.OnClickListener() {

    private SharedPreferences settings;
    UserUtil user = new UserUtil(PayPalHandler.this);

    @Override
    public void onClick(View v) {
        settings = getSharedPreferences(Constants.SETTINGS_FILE_NAME, Context.MODE_PRIVATE);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String price = intent.getStringExtra("price");
        String dealCode = intent.getStringExtra("dealCode");
        String token=PayPalHandler._deviceReferenceToken;
        String currentSecurityToken = settings.getString(Constants.SECURITY_TOKEN, null);
        String os = "Android";
        String provider = "paypal";
        String caltureCode = "he-IL";
        String phoneModel = android.os.Build.MODEL;
        String androidVersion = android.os.Build.VERSION.RELEASE;
        double lat = user.getSelfLocation().getLatitude();
        double lon = user.getSelfLocation().getLongitude();

        // here i need to built the string according to parameters like deal code, email, deal title etc

        String buf = "http://api.gooz.co.il/beginpurchase.ashx?request=%7b%22ReturnUrl%22%3a%22http%3a%2f%2flocalhost%3a1295%2fSALI.Server.Web.he-IL%2fThanks.aspx%3fDealCode%3d4%22%2c%22CancelUrl%22%3a%22http%3a%2f%2flocalhost%3a1295%2fSALI.Server.Web.he-IL%2fDealInfo.aspx%3fDealCode%3d4%22%2c%22DealUrl%22%3a%22http%3a%2f%2flocalhost%3a1295%2fSALI.Server.Web.he-IL%2fDealInfo.aspx%3fDealCode%3d4%22%2c%22ShopperEmail%22%3a%22sagi.baron76%40gmail.com%22%2c%22CultureCode%22%3a%22he-IL%22%2c%22Device%22%3a%22android%22%2c%22Provider%22%3a%22paypal%22%2c%22ForceJson%22%3afalse%2c%22PurchaseArgs%22%3a%5b%7b%22DealCode%22%3a%224%22%2c%22Quantity%22%3a%221%22%2c%22PaymentNumber%22%3a%221%22%7d%5d%7d";

        //Construct the WebView
        _webView = new WebView(getApplicationContext());


        WebViewClient webClient = new WebViewClient()
           {
               // Override page so it's load on my view only
               @Override
               public boolean shouldOverrideUrlLoading(WebView  view, String  url)
               {
                return false;
               }
           };
        //Set our view to the WebView
        _webView.getSettings().setJavaScriptEnabled(true);
        setContentView(_webView);

        _webView.setWebViewClient(webClient);
        _webView.requestFocusFromTouch();
        _webView.loadUrl(buf);

    }
};

}

4

0 回答 0