要将PayPal embedded simple payments集成到安卓应用中,可以按照以下步骤进行:
dependencies {
implementation 'com.paypal.sdk:paypal-android-sdk:2.16.0'
}
PayPalConfiguration config = new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
.clientId("YOUR_CLIENT_ID");
PayPalPayment payment = new PayPalPayment(new BigDecimal("10.00"), "USD", "Item Name",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, REQUEST_CODE_PAYPAL_PAYMENT);
<uses-permission android:name="android.permission.INTERNET" />
<application>
<service
android:name="com.paypal.android.sdk.payments.PayPalService"
android:exported="false" />
</application>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PAYPAL_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirmation != null) {
String paymentId = confirmation.getProofOfPayment().getPaymentId();
// 处理支付成功逻辑
}
} else if (resultCode == Activity.RESULT_CANCELED) {
// 处理支付取消逻辑
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
// 处理支付无效逻辑
}
}
}
以上是将PayPal embedded simple payments集成到安卓应用中的基本步骤。在实际应用中,你可能还需要根据具体需求进行更多的定制和处理。
领取专属 10元无门槛券
手把手带您无忧上云