如果您使用的是安卓工作室。然后只需将 paypal sdk 添加到您的应用级 build.gradle 文件中。
compile 'com.paypal.sdk:paypal-android-sdk:2.14.2'
现在您可以使用以下代码接受付款。请记住,您还需要您的贝宝客户端 ID。您可以通过在 developer.paypal.com 上创建一个应用来获取它
private void getPayment() {
//Getting the amount from editText
paymentAmount = editTextAmount.getText().toString();
//Creating a paypalpayment
PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Simplified Coding Fee",
PayPalPayment.PAYMENT_INTENT_SALE);
//Creating Paypal Payment activity intent
Intent intent = new Intent(this, PaymentActivity.class);
//putting the paypal configuration to the intent
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
//Puting paypal payment to the intent
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
//Starting the intent activity for result
//the request code will be used on the method onActivityResult
startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}
活动结果代码将是
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//If the result is from paypal
if (requestCode == PAYPAL_REQUEST_CODE) {
//If the result is OK i.e. user has not canceled the payment
if (resultCode == Activity.RESULT_OK) {
//Getting the payment confirmation
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
//if confirmation is not null
if (confirm != null) {
try {
//Getting the payment details
String paymentDetails = confirm.toJSONObject().toString(4);
Log.i("paymentExample", paymentDetails);
//Starting a new activity for the payment details and also putting the payment details with intent
startActivity(new Intent(this, ConfirmationActivity.class)
.putExtra("PaymentDetails", paymentDetails)
.putExtra("PaymentAmount", paymentAmount));
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
}
}
来源:Android PayPal Integration Tutorial