最新发现Paypal的官方SDK已经不再维护了,所以需要把项目的支付做一下升级。
文档链接:点击这里
根据文档来看Paypal支付的集成相比以前简单了许多,下面我们讲一下集成步骤:
1:在 build.gradle 中添加以下内容
compile 'com.braintreepayments.api:braintree:2.+'
2:在你的 AndroidManifest.xml 中添加
<activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask">
<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="${applicationId}.braintree" />
</intent-filter>
</activity>
3:从服务器获取Token
根据后台提供的 url 获取到Token 后台 集成链接
4:初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paypal);
//初始化并添加监听
try {
mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
mBraintreeFragment.addListener(new ConfigurationListener() {
@Override
public void onConfigurationFetched(Configuration configuration) {
}
});
// 支付完成监听
mBraintreeFragment.addListener(new PaymentMethodNonceCreatedListener() {
@Override
public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
// Send nonce to server
String nonce = paymentMethodNonce.getNonce();
postNonceToServer(nonce);
if (paymentMethodNonce instanceof PayPalAccountNonce) {
PayPalAccountNonce payPalAccountNonce = (PayPalAccountNonce) paymentMethodNonce;
// Access additional information
String email = payPalAccountNonce.getEmail();
String firstName = payPalAccountNonce.getFirstName();
String lastName = payPalAccountNonce.getLastName();
String phone = payPalAccountNonce.getPhone();
// See PostalAddress.java for details
PostalAddress billingAddress = payPalAccountNonce.getBillingAddress();
PostalAddress shippingAddress = payPalAccountNonce.getShippingAddress();
}
}
});
// 取消监听
mBraintreeFragment.addListener(new BraintreeCancelListener() {
@Override
public void onCancel(int requestCode) {
}
});
// 错误监听
mBraintreeFragment.addListener(new BraintreeErrorListener() {
@Override
public void onError(Exception error) {
if (error instanceof ErrorWithResponse) {
ErrorWithResponse errorWithResponse = (ErrorWithResponse) error;
BraintreeError cardErrors = errorWithResponse.errorFor("creditCard");
if (cardErrors != null) {
// There is an issue with the credit card.
BraintreeError expirationMonthError = cardErrors.errorFor("expirationMonth");
if (expirationMonthError != null) {
// There is an issue with the expiration month.
LogUtils.e("paypal",expirationMonthError.getMessage());
}
}
}
}
});
// mBraintreeFragment is ready to use!
} catch (InvalidArgumentException e) {
// There was an issue with your authorization string.
}
}
5:调起支付
yourButton.setOnClickListener(new View.OnClickListener(){
@Override public void onClick (View view){
setupBraintreeAndStartExpressCheckout();
}
})
public void setupBraintreeAndStartExpressCheckout() {
PayPalRequest request = new PayPalRequest(“价格”)
.currencyCode("货币符号")
.intent(PayPalRequest.INTENT_SALE);
PayPal.requestOneTimePayment(mBraintreeFragment, request);
}
这里注意三种Intent的意思 INTENT_SALE 表示立即到账。
5:付款成功后访问服务器验证
private void postNonceToServer(String nonce) {
final Map<String, String> dataMap = new HashMap<>();
dataMap.put("nonce", nonce);
dataMap.put("amount", price);
}
验证的接口由后台给这里逻辑由后台处理,这里返回成功,说明支付并验证成功。
大家测试的时候一定要用美国地址的paypal进行测试哈。