PayPal与Android集成 [英] PayPal Integration with Android

查看:499
本文介绍了PayPal与Android集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些相关的问题,但都没有关注我所遇到的具体问题:

I have seen some related questions but none focusing on the specific problem I have:

我正在使用PayPal MPL库.

I'm using the PayPal MPL Library.

我构建我的PayPalPayment对象,然后创建要进行结帐的活动.很好我的问题是,在ResultDelegate上,我需要从活动中调用一个函数,该函数会在付款后发生并进行一些更改(例如存储SharedPreferences等).

I build my PayPalPayment object, then create the activity for the checkout to occur. That runs fine. My problem is, on the ResultDelegate I need to call a function from my activity, that occurs after the payment and makes some changes (such as storing SharedPreferences, etc.).

是这样的:

public class ResultDelegate implements PayPalResultDelegate, Serializable {
    public void onPaymentSucceeded(String payKey, String paymentStatus) {
        System.out.println("SUCCESS, You have successfully completed your transaction.");
        System.out.println("PayKey: "+payKey);
        System.out.println("PayStatus: "+paymentStatus);

        callMyCustomAfterPaymentFunction();
    }
    ...
}

现在的事情是,我试图为ResultDelegate创建一个接受我的活动的构造函数.我现有的代码是:

Now the thing is, I tried to create a constructor for ResultDelegate that accepts my activity. My existing code is:

//On the activity class
public class MainMenuActivity extends Activity {
    public void onCreate(Bundle savedInstanceState)
    {
        ...
        Button buy = (Button) findViewByID(R.id.buy_button);
        buy.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                new PurchaseTask(activity).execute();
            }
        }
    }
}

public class PurchaseTask extends AsyncTask <String, Void, String> {
    protected String doInBackground()
    {
        ...
        PayPal pp = PayPal.getInstance();
        CheckoutButton cb = pp.getCheckoutButton(...);
        cb.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                ResultDelegate delegate = new ResultDelegate(myActivity);
                Intent checkout = PayPal.getInstance().checkout(paument, activity, delegate);
                activity.StartActivity(checkoutIntent);
            }
        }
    }
}

//On the ResultDelegate class

public class ResultDelegate implements PayPalResultDelegate, Serializable {
    private Activity myActivity;

    public void onPaymentSucceeded(String payKey, String paymentStatus) {
        System.out.println("SUCCESS, You have successfully completed your transaction.");
        System.out.println("PayKey: "+payKey);
        System.out.println("PayStatus: "+paymentStatus);

        myActivity.performAfterPaymentOperations();
    }
    ...
}

因此,目标是从ResultDelegate调用活动函数.甚至更简单,只是为了能够在ResultDelegate onPaymentSucceeded()触发时存储一些SharedPreference更改.

So the goal is to call the activity function from the ResultDelegate. Or even simpler, just to be able to store some SharedPreference changes when the ResultDelegate onPaymentSucceeded() fires.

但是我收到一个NotSerializableException异常,提到我的MyActivity字段不可序列化.

But I get a NotSerializableException mentioning that the my MyActivity field is not Serializable.

因此,然后我将瞬态标识符添加到ResultDelegate内的我的活动字段中,但是现在我得到了NullPointerException.

So, then I added the transient identifier to my activity field inside the ResultDelegate, but now I get a NullPointerException.

推荐答案

贝宝(Paypal)网站上提供的实施方式与您的实施方式不同.他们正在执行startActivityForResult()以启动PaypalActivity.当使用onActivityResult()方法时,他们正在检查statusCode以检查交易状态并采取相应措施.

Implementation provided on paypal website is different from yours. They are doing startActivityForResult() to start PaypalActivity. and when in onActivityResult() method they are checking statusCode to check transaction status and act accordingly.

请按照该文档进行实施.

Follow that document for your implementation.

在您的代码中,我找不到使用AsyncTask的要点.您的ResultDelegateSerializable,其中Activity并非是为什么它抛出NotSerializableException的原因.

Here in your code, I donot find a point for using AsyncTask. Your ResultDelegate is Serializable where as Activity is not thats why it is throwing NotSerializableException.

在为Google Android平台开发时,为什么不使用Google Checkout应用内?

As you are developing for Google Android platform, then why not to use Google Checkout In-App?

PaypalActivity完成时将调用此方法.该活动会将resultCode传递给该onActivityResult方法.

This method will be called when your PaypalActivity will finish. That activity will pass resultCode to this onActivityResult method.

@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.
    }
}

致谢, 阿奇夫·哈米德(Aqif Hamid)

regards, Aqif Hamid

这篇关于PayPal与Android集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆