如何使用webview将paypal集成到Android应用程序

时间:2013-05-16 02:59:00

标签: android android-intent paypal paypal-nvp

我想将paypal集成到我的应用中。在我看到这个时,我快到了那里:https://developer.paypal.com/webapps/developer/support/faq#non-US-dev Paypal不允许非美国开发人员使用REST API。所以我留下的选择是经典的api。这就是问题所在。我不知道如何使用它们。

我打算使用NVP api。我不知道怎么做但下面是我计划做的方式的流程图。我需要在checkout.java上设置项目编号,描述,价格等,这将发送到www.domain.com/setsessions.php

我不确定它是否会起作用。但即便如此,我也不知道新的'意图'如何检测用户是否已成功付款,付款失败或取消付款。

App paypal flow

我需要帮助。这真是令人沮丧......已经尝试将paypal整合近2周。感觉非常无益......

这甚至可以吗?还是有更好的方法? 我的要求是设置发票编号,项目编号,描述,价格,位置,这些都是应用程序所需的。

1 个答案:

答案 0 :(得分:5)

MainActivity.java

public class PizzaMain extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        PayPal ppObj = PayPal.initWithAppID(this.getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_NONE);

        CheckoutButton launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_152x33, CheckoutButton.TEXT_PAY);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        params.addRule(RelativeLayout.CENTER_HORIZONTAL);

        launchPayPalButton.setLayoutParams(params);
        launchPayPalButton.setOnClickListener(this);

        ((RelativeLayout) findViewById(R.id.mRlayout1)).addView(launchPayPalButton);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        PayPalPayment newPayment = new PayPalPayment();
        char val[] = { '5', '0' };
        BigDecimal obj_0 = new BigDecimal(val);
        newPayment.setSubtotal(obj_0);
        newPayment.setCurrencyType("USD");
        newPayment.setRecipient("my@email.com");
        newPayment.setMerchantName("My Company");
        Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this);
        this.startActivityForResult(paypalIntent, 1);
    }

    @SuppressWarnings("unused")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case Activity.RESULT_OK:
            // The payment succeeded
            String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            // Tell the user their payment succeeded
            break;
            case Activity.RESULT_CANCELED:
            // The payment was canceled
            // Tell the user their payment was canceled
            break;
            case PayPalActivity.RESULT_FAILURE:
            // The payment failed -- we get the error from the EXTRA_ERROR_ID
            // and EXTRA_ERROR_MESSAGE
            String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
            String errorMessage = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
            // Tell the user their payment was failed.
            break;
        }
    }
}

并在AndroidManifest.xml文件中提供以下权限。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

将Activity添加到AndroidManifest.xml文件中。

<activity
    android:name="com.paypal.android.MEP.PayPalActivity"
    android:configChanges="keyboardHidden|orientation"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

将PayPal_MPL.jar Jar文件作为参考库添加到您的应用程序中