3

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

我正在使用 PayPal MPL 库。

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

所以是这样的:

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 创建一个接受我的活动的构造函数。我现有的代码是:

//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 更改。

但是我得到一个 NotSerializableException 提到我的 MyActivity 字段不是可序列化的。

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

4

3 回答 3

3

贝宝移动结账指南

贝宝网站上提供的实施与您的不同。他们正在做startActivityForResult()开始PaypalActivity。并且在onActivityResult()方法中,他们正在检查statusCode以检查交易状态并采取相应行动。

按照该文档进行实施。

在您的代码中,我没有找到使用AsyncTask. 你ResultDelegateSerializable哪里,Activity这不是它抛出的原因NotSerializableException

编辑:

当您正在为 Google Android 平台开发时,为什么不使用 Google Checkout In-App 呢?

编辑:

PaypalActivity当您完成时,将调用此方法。该活动将传递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.
    }
}

问候, 阿基夫·哈米德

于 2012-06-30T21:26:26.633 回答
1

您可以像这样创建自定义侦听器:

创建一个自定义监听器:

  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;

    }

现在像这样在任何你想要的地方调用它:

mListener.onEvent(Uri, 1));

现在,每当您调用此侦听器时,这将在您使用此侦听器的活动中触发,如下所示:

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

        }
    });

myCanvas 是您创建侦听器的类的对象。

于 2012-07-04T12:16:20.807 回答
0

试试这个解决方案:

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);
                }
于 2016-07-22T12:42:12.613 回答