PayPal期货付款活动授权无效

时间:2017-12-23 16:15:28

标签: android paypal paypal-sandbox

我正在使用PayPal移动SDK(Android),因为我需要访问REST API,而我正在使用Sandbox。即时付款工作正常,但我无法获得未来付款的授权码。我检查了开发人员门户网站并启用了未来付款,但我认为这是Sandbox的默认设置。我正在使用Xamarin android(c#):

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="ShoezApp.Android">
  <!--This will default to ShoezApp.ShoezApp if no second part is specified-->
  <uses-sdk android:minSdkVersion="19" />
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  <!-- admob permissions-->
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


  <application android:label="ShoezApp" android:icon="@drawable/icon">
    <activity android:name="com.microsoft.windowsazure.mobileservices.authentication.RedirectUrlActivity" android:launchMode="singleTop" android:noHistory="true">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="shoezapp" android:host="easyauth.callback" />
      </intent-filter>
    </activity>

    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
    <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
  </application>
</manifest>

代码:

public class PayPalDroid : IPayPalService
    {
        private PayPalConfiguration config = new PayPalConfiguration()
            .Environment(PayPalConfiguration.EnvironmentSandbox)
            .ClientId("AeX06w97L702sEFGK5ZB4Tc4Veyo4oOzopS9DgKiHCDVgD4O0mGeZoNl1t-sFWAmRXyzyx87Y1mupJ1W")
            .MerchantName("Example Merchant")
            .MerchantPrivacyPolicyUri(Android.Net.Uri.Parse("https://www.example.com/privacy"))
            .MerchantUserAgreementUri(Android.Net.Uri.Parse("https://www.example.com/legal"));

        //only calls default constructor with dependency injection, so this is necessary
        public PayPalDroid()  {}

        private int REQUEST_CODE_PAYMENT = 1;
        private int REQUEST_CODE_FUTURE_PAYMENT = 2;
        private int REQUEST_CODE_PROFILE_SHARING = 3;

        public Task<string> RequestPayPal()
        {
            Context context = MainActivity.Instance;
            MainActivity activity = (MainActivity)context;
            //create the listener
            var listener = new ActivityResultListener(activity);

            // start paypal service - this must be done each time
            var intent = new Intent(context, typeof(PayPalService));
            intent.PutExtra(PayPalService.ExtraPaypalConfiguration, config);
            context.StartActivity(Intent.CreateChooser(intent, "Request PayPal transaction"));

            var payment = new PayPalPayment(new Java.Math.BigDecimal("2.45"), "USD", "the item",
                PayPalPayment.PaymentIntentSale);

            intent = new Intent(context, typeof(PaymentActivity));
            intent.PutExtra(PayPalService.ExtraPaypalConfiguration, config);
            intent.PutExtra(PaymentActivity.ExtraPayment, payment);

            activity.StartActivityForResult(Intent.CreateChooser(intent, "PayPal transaction complete"), REQUEST_CODE_FUTURE_PAYMENT);

            return listener.Task;
        }


        private class ActivityResultListener
        {
            private TaskCompletionSource<string> Complete = new TaskCompletionSource<string>();
            public Task<string> Task { get { return this.Complete.Task; } }

            MainActivity Activity;

            public ActivityResultListener(MainActivity activity)
            {
                Activity = activity;
                // subscribe to activity results
                activity.ActivityResult += OnActivityResult;
            }

            private void OnActivityResult(int requestCode, Result resultCode, Intent data)
            {
                // unsubscribe from activity results
                Activity.ActivityResult -= OnActivityResult;

                // process result
                if (resultCode == Result.Ok)
                {
                    Object auth = data.GetParcelableExtra(PayPalFuturePaymentActivity.ExtraResultAuthorization);
                    if (auth != null) //**THIS IS ALWAYS NULL**//
                    {

                        try
                        {
                            //string authorization_code = auth.AuthorizationCode;

                            //sendAuthorizationToServer(auth);

                        }
                        catch (Exception e)
                        {
                            //Log.e("FuturePaymentExample", "an extremely unlikely failure occurred: ", e);
                        }
                    }
                }

                Complete.TrySetResult(resultCode.ToString());

                Context context = MainActivity.Instance;
                var intent = new Intent(context, typeof(PayPalService));
                context.StopService(intent);
            }
        }
    }

在没有授权码的情况下,我无法继续(即服务器端编码)。问题是'auth'对象总是为null,我看不出代码有什么问题,因为它基本上是示例代码。我错过了什么?或者这仅适用于现场环境? 感谢。

1 个答案:

答案 0 :(得分:1)

犯了一个简单的错误,活动需要是PayPalFuturePaymentActivity,而不是PaymentActivity。 PaymentConfirmation对象可用于PaymentActivity,而不是PayPalAuthorization对象。

我在这里找到了一些非常好的PayPal集成示例代码,这可能对其他人有帮助:https://www.csharpcodi.com/vs2/?source=176/PayPal.Forms/MobileApps/PayPal.Forms/PayPalAndroidTest/PayPalManager.cs