简体   繁体   English

PayPal与Android集成

[英]PayPal Integration with Android

I have seen some related questions but none focusing on the specific problem I have: 我已经看到了一些相关的问题,但是没有一个针对我所遇到的具体问题:

I'm using the PayPal MPL Library. 我正在使用PayPal MPL库。

I build my PayPalPayment object, then create the activity for the checkout to occur. 我建立我的PayPalPayment对象,然后创建要进行结帐的活动。 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.). 我的问题是,在ResultDelegate上,我需要从我的活动中调用一个函数,该函数在付款后发生并进行一些更改(例如存储SharedPreferences等)。

So something like this: 所以像这样:

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();
    }
    ...
}

Now the thing is, I tried to create a constructor for ResultDelegate that accepts my activity. 现在的事情是,我试图为ResultDelegate创建一个接受我的活动的构造函数。 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();
    }
    ...
}

So the goal is to call the activity function from the ResultDelegate. 因此,目标是从ResultDelegate调用活动函数。 Or even simpler, just to be able to store some SharedPreference changes when the ResultDelegate onPaymentSucceeded() fires. 甚至更简单,就是在ResultDelegate onPaymentSucceeded()触发时能够存储一些SharedPreference更改。

But I get a NotSerializableException mentioning that the my MyActivity field is not Serializable. 但是我收到一个NotSerializableException,提到我的MyActivity字段不可序列化。

So, then I added the transient identifier to my activity field inside the ResultDelegate, but now I get a NullPointerException. 因此,然后我将瞬态标识符添加到ResultDelegate内的活动字段中,但是现在我得到了NullPointerException。

Paypal Mobile Chekout guide 贝宝移动Chekout指南

Implementation provided on paypal website is different from yours. 贝宝网站上提供的实施方法与您的不同。 They are doing startActivityForResult() to start PaypalActivity . 他们正在执行startActivityForResult()以启动PaypalActivity and when in onActivityResult() method they are checking statusCode to check transaction status and act accordingly. 当在onActivityResult()方法中时,他们正在检查statusCode以检查交易状态并采取相应措施。

Follow that document for your implementation. 请按照该文档进行实施。

Here in your code, I donot find a point for using AsyncTask . 在您的代码中,我找不到使用AsyncTask的要点。 Your ResultDelegate is Serializable where as Activity is not thats why it is throwing NotSerializableException . 您的ResultDelegate是可Serializable ,因为Activity不是,这就是为什么它NotSerializableException

Edit: 编辑:

As you are developing for Google Android platform, then why not to use Google Checkout In-App? 在为Google Android平台开发时,为什么不使用Google Checkout应用程序内?

Edit: 编辑:

This method will be called when your PaypalActivity will finish. 当您的PaypalActivity完成时,将调用此方法。 That activity will pass resultCode to this onActivityResult method. 该活动会将resultCode传递给此onActivityResult方法。

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

regards, Aqif Hamid 问候,Aqif Hamid

You can create you custom listener something like this : 您可以创建自定义侦听器,如下所示:

Create a custom listener : 创建一个自定义侦听器:

  OnDoubleTap mListener;

    // Double tap custome listenre to edit photoes
    public interface OnDoubleTap {
        public void onEvent(Uri imgPath, int mPos);

    }

    public void setDoubleTapListener(OnDoubleTap eventListener) {
        mListener = eventListener;

    }

Now call this wherever you want like this : 现在,您可以在任何这样的地方调用它:

mListener.onEvent(Uri, 1));

Now whenever you call this listener this will fire in your activity where you use this listener like this : 现在,无论何时调用此监听器,都会在您使用此类监听器的活动中触发:

myCanvas.setDoubleTapListener(new OnDoubleTap() {

        @Override
        public void onEvent(Uri imgPath, int Pos) {
            // TODO Auto-generated method stub
            Toast.makeText(mContext, "LISTENER WORKING !!!", Toast.LENGTH_SHORT).show();

        }
    });

Where myCanvas is object of class where you create you listener. 其中myCanvas是创建侦听器的类的对象。

Try this solution: 试试这个解决方案:

PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(price),getResources().getString(R.string.curruncy_code), getResources().getString(R.string.app_name),
                    PayPalPayment.PAYMENT_INTENT_SALE);
            Intent intent = new Intent(CreateEventStep4.this, PaymentActivity.class);
            intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
            startActivityForResult(intent, REQUEST_PAYPAL_PAYMENT);

PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {

                    Log.e("paymentExample", confirm.toJSONObject().toString());


                    JSONObject jsonObj=new JSONObject(confirm.toJSONObject().toString());

                    String paymentId=jsonObj.getJSONObject("response").getString("id");
                    System.out.println("payment id:-=="+paymentId);

                    screenShotFile = takeScreenshot(frmTemplate);
                    uploadImage(myEvent.getEvent_id(),screenShotFile);

                } catch (JSONException e) {
                    Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM