我知道很多关于paypal按钮的讨论,但我不明白什么需要..假设这是我的paypal ID B7REJHRY9RGWL
...我现在要做什么呢?是否有一段代码来创建按钮?我的目标是创建一个免费的捐赠按钮并将其插入我的Donate.java
活动中。谢谢
由于Paypal收购了Braintrain,Paypal已经对其PayPal Mobile SDK进行了折旧,因此上一个答案的链接不再适用。
截至2019年3月,PayPal支持称为Paypal Checkout SDK的移动应用程序的轻量级客户端集成。
首先,您需要设置SDK
如果尚未完成,Android应用程序需要指定它接受Internet权限。如果您还想在钱包中添加触觉反馈以供选择通知,请在应用程序的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网址相匹配。
请记住,App Link需要在PayPal Developer Portal中注册为返回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重定向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 Intercept
如果先前的集成与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());
//sample Web view intercept.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 Experience。
结帐完成后设置回叫代理。
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