【问题标题】:What needs to create paypal button in android?在android中创建paypal按钮需要什么?
【发布时间】:2013-08-29 21:13:05
【问题描述】:

我知道有很多关于贝宝按钮的讨论,但我不明白需要什么。假设这是我的贝宝 ID B7REJHRY9RGWL... 我现在有什么用呢?是否有一段代码可以创建按钮?我的目标是创建一个免费捐赠按钮并将其插入我的Donate.java 活动中。谢谢

【问题讨论】:

    标签: android paypal android-activity android-button donations


    【解决方案1】:

    自从 Paypal 收购 Braintrain 以来,Paypal 已经贬低了其 PayPal Mobile SDK,因此上一个答案中的链接不再有效。

    自 2019 年 3 月起,PayPal 支持名为 Paypal Checkout SDK 的移动应用轻量级客户端集成。

    首先您需要为 SDK 进行设置

    如果尚未完成,Android 应用程序需要指定它接受互联网权限。如果您还想在钱包中添加触觉反馈用于选择通知,请在应用程序中的 AndroidManifest.xml 顶部添加振动权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="checkout.paypal.com.myapplication">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.VIBRATE"/>
    

    对于 PayPal 进行身份验证并在应用程序中被记住,我们依赖于 OpenID 的 AppAuth 实现。请将此活动添加到 AndroidManifest.xml 以确保使用 App Links 切换回应用程序,下面列出的 url 应与生成的 App Links url 匹配。

    请记住,应用链接需要在 PayPal 开发者门户中注册为返回 URL。

    <activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
    
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
    
            <data
                android:scheme="https"
                android:host="example.com"
                android:path="/buyingstuff"/>
        </intent-filter>
    </activity>
    

    如果无法为特定用户加载本机体验,PayPal 将通过回退到 Chrome 自定义标签(或默认浏览器)来确保发生转化。为确保用户从 PayPal Web 体验返回到应用程序,采用了自定义方案。请使用您为应用程序选择的自定义方案注册活动。主机应留在paypalxo

    <activity android:name="com.paypal.pyplcheckout.PYPLCheckoutReceiver"
      android:launchMode="singleTask"
      >
      <intent-filter android:autoVerify="true">
    
          <data
              android:scheme="testapp"
              android:host="paypalxo" />
    
          <action android:name="android.intent.action.VIEW" />
    
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
    
      </intent-filter>
    </activity>
    

    完成的 AndroidManifest.xml 应类似于以下内容。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="checkout.paypal.com.myapplication">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.VIBRATE"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
                <intent-filter android:autoVerify="true">
                    <action android:name="android.intent.action.VIEW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
    
                    <data
                        android:scheme="https"
                        android:host="example.com"
                        android:path="/buyingstuff"/>
                </intent-filter>
            </activity>
    
            <activity android:name="com.paypal.pyplcheckout.PYPLCheckoutReceiver"
              android:launchMode="singleTask"
              >
              <intent-filter android:autoVerify="true">
    
                  <data
                      android:scheme="testapp"
                      android:host="paypalxo" />
    
                  <action android:name="android.intent.action.VIEW" />
    
                  <category android:name="android.intent.category.DEFAULT" />
                  <category android:name="android.intent.category.BROWSABLE" />
    
              </intent-filter>
            </activity>
        </application>
    </manifest>
    

    然后您需要将存储库添加到您的构建文件中。使用 Gradle,它看起来像:

    allprojects {
        repositories {
            google()
            jcenter()
            maven { url "https://github.com/syrjs/maven/raw/master"}
            maven { url "https://github.com/paypal/paypalcheckout-android/raw/nativeSDK"}
        }
    }
    

    将依赖项添加到应用级别的 build.gradle。

    dependencies {
        implementation 'com.paypal.pyplcheckout:nativexo:3.4.5'
    }
    

    下一步是调用 SDK。 PayPal既可以在应用程序中观看WebView,提供原生支付体验来代替网站,也可以通过调用简单函数直接调用。您可以使用以下步骤手动集成到应用程序中。

    SDK 需要一些关于应用程序的额外信息才能被调用。在 AndroidManifest.xml 中提供自定义方案设置,以及在 AndroidManifest.xml 中设置的 App Link Redirect URL。提供用于此应用程序的客户端 ID。运行环境也可以设置。

    final PYPLCheckoutEnvironment pyplEnvironment = PYPLCheckoutEnvironment.getInstance();
    
    pyplEnvironment.setkPYPLEnvironment(Environment.SANDBOX);
    
    pyplEnvironment.setkPYPLUrlScheme("foobarstore");
    
    //set the redurect uri, that has the assetLinks.json.
    pyplEnvironment.setkPYPLRedirectURL("https://paypalmerchant.herokuapp.com/thankyou");
    
    //set the client ID for the merchant
    pyplEnvironment.setClientId("AX93NErgg-F0VeBQ6pNLwa2VKQdw3BnKDvBnasIe_pKoprQyz6NiSf6XS7I1Qtro-VD4GP-AJdjT0Uz4");
    
    //set the user context. 'this' should be the activity from which the experience is being called.
    pyplEnvironment.setkPYPLUserContext(this);
    

    下一步是集成 WebView 拦截

    如果前面的集成与 WebView 周围的代码发生冲突,则可以手动拦截 WebView,如这些示例所示。

    PYPLCheckout.getInstance().shouldOverrideUrlLoading(view, url); 方法可用于拦截任何重定向到 PayPal 的 webView。此函数返回一个布尔值,可用于 webViewClient 中的 shouldOverrideUrlLoading() 方法。

    WebView 与您自己的 webViewClient 集成的示例。 //MainActivity.class

    //在您要调用体验的活动中。 WebView webView = new WebView(this);

    //SampleWebViewIntercept 是你的 webViewClient。 webView.setWebViewClient(new SampleWebViewIntercept());

    //SampleWebViewIntercept.class

    public class SampleWebViewIntercept extends WebViewClient {
    
     //include this for integrating with Checkout.js
      @Override
      public void onPageFinished(WebView view, String url) {
    
          super.onPageFinished(view, url);
    
          //this will load a script to handle the Checkout.js integration
          PYPLCheckout.getInstance().loadScript(view);
    
      }
    
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, final String url) {
    
          return PYPLCheckout.getInstance().shouldOverrideUrlLoading(view, url);
    
      }
    
    }
    

    直接调用

    通过从后台系统提供支付令牌直接启动 PayPal Checkout 体验。

    结帐完成后设置回调委托。

    pyplEnvironment.setkCheckoutDelegate(new PYPLCheckoutDelegate() {
    
          @Override
          public void completeCheckout(HashMap<String,String> returnParams) {
    
            //return params will contain all the required information for the merchant to finish the transaction
              Log.i("CheckoutFinishedWith>>", returnParams.toString());
    
            //here is a sample of what return params consists
            /**
            {
              from_cart=true, returnUrl=https://sampleurl.com/checkouts/?from_cart=true&key=Key&token=EC-token&PayerID=payerID&opType=payment,
              token=EC-token,
              key=Key,
              PayerID=payerID,
              opType=payment
            }
            **/
    
          }
    
          // in addition to the checkoutComplete delegate you can also provide a canceled delegate that is called when the user exits CCT amidst checkout
          @Override
           public void checkoutCanceled() {
    
              Log.i("Checkout Canceled>>", "Checkout Canceled");
    
            }
    
    
    
      });
    

    设置您需要传递给 PayPal 的其他参数。这是可选的。

    //every option should be a string with the key and value joined with a '='
    String[] pyplParamsArray = {"useraction=commit"};
    
    pyplEnvironment.setkPYPLQueryStringParameters(pyplParamsArray);
    

    使用结帐令牌开始体验。

    PYPLCheckout.getInstance().startCheckoutWithECToken(MainActivity.this, "EC-1FP91222RL3429812");
    

    删除委托。如果您需要从我们的回调中删除委托,您可以使用

    final PYPLCheckoutEnvironment pyplEnvironment = PYPLCheckoutEnvironment.getInstance();
    pyplEnvironment.clearCheckoutDelegate();
    

    要再次接收事件,只需再次设置委托。

    参考: https://paypal.github.io/paypalnativecheckout-docs/Android/integrating_experience/#prerequisites

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2012-07-21
        • 2017-06-24
        • 2017-01-04
        • 2014-08-21
        • 1970-01-01
        • 2011-10-01
        • 2023-03-12
        • 2020-05-28
        • 2010-11-20
        相关资源
        最近更新 更多