因為最近項目中需要集成paypal,所以集成了一下,發(fā)現(xiàn)網(wǎng)上的有些文章過于老舊,所以自己寫一篇踩坑記,首先是去官網(wǎng)申請賬號,到時候會分配給你一個client_id
以上準備都做好了 就新建一個項目
- 引入paypel的包
compile('com.paypal.sdk:paypal-android-sdk:2.15.3')
{ exclude group: 'io.card' }// 如果不想支持信用卡支付 就加上這句話
- 新建一個activity 配置
private static PayPalConfiguration config =
new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
.clientId("<YOUR_CLIENT_ID>");
這里就輸入你的client_id
3.然后在oncreate中開啟服務(wù)
// 開啟PayPal服務(wù)
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig);
startService(intent);
4.買東西
public void onBuyPressed(View pressed) {
//創(chuàng)建支付對象,用于傳過去給PayPal服務(wù)器進行收款
PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
//這里直接調(diào)起PayPal的sdk進行付款操作
startActivityForResult(intent, 1);
}
//這里只傳一個總價格或者單個產(chǎn)品的信息收款情況
private PayPalPayment getThingToBuy(String paymentIntent) {
return new PayPalPayment(new BigDecimal("0.01"), "USD", "sample item",
paymentIntent);
}
//這里是購買一系列產(chǎn)品創(chuàng)建購買對象
private PayPalPayment getStuffToBuy(String paymentIntent) {
PayPalItem[] items =
{
new PayPalItem("sample item #1", 2, new BigDecimal("87.50"), "USD",
"sku-12345678"),
new PayPalItem("free sample item #2", 1, new BigDecimal("0.00"),
"USD", "sku-zero-price"),
new PayPalItem("sample item #3 with a longer name", 6, new BigDecimal("37.99"),
"USD", "sku-33333")
};
BigDecimal subtotal = PayPalItem.getItemTotal(items);
BigDecimal shipping = new BigDecimal("7.21");
BigDecimal tax = new BigDecimal("4.67");
PayPalPaymentDetails paymentDetails = new PayPalPaymentDetails(shipping, subtotal, tax);
BigDecimal amount = subtotal.add(shipping).add(tax);
PayPalPayment payment = new PayPalPayment(amount, "USD", "sample item", paymentIntent);
payment.items(items).paymentDetails(paymentDetails);
//--- set other optional fields like invoice_number, custom field, and soft_descriptor
payment.custom("This is text that will be associated with the payment that the app can use.");
return payment;
}
5.接收回調(diào)
/**
* 接收支付結(jié)果的回調(diào).
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm =
data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
//這里可以把PayPal帶回來的json數(shù)據(jù)傳給服務(wù)器以確認你的款項是否收到或者收全
//可以直接把 confirm.toJSONObject() 這個帶給服務(wù)器,
//得到服務(wù)器返回的結(jié)果,你就可以跳轉(zhuǎn)成功頁面或者做相應的處理了
Log.i(TAG, confirm.toJSONObject().toString(4));
// confirm.toJSONObject().toString();
Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));
Log.i(TAG, confirm.toJSONObject().toString());
} catch (JSONException e) {
Log.e(TAG, "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i(TAG, "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(
TAG,
"An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
}
6.注銷service
@Override
protected void onStop() {
stopService(new Intent(this, PayPalService.class));
super.onStop();
}