PayPal Android SDK 2.0 支付

1.用户使用PayPal账户或信用卡支付,支付完成接收返回一个支付ID。

2.在你的服务器端,使用PayPal的API获取付款详情,进行支付验证。

准备

开始之前需要做以下准备:

1. 要有个PayPal账号,且注册为商户。

3. 登录https://developer.paypal.com,,注册Sandbox测试账号(买家卖家)。

2. 集成到你的应用中,在https://developer.paypal.com创建一个APP,并选择刚创建好的Sandbox账号,

APP会生成有ClientID和secret。

4. 提交在Sandbox测试好的应用,PayPal会对你的应用进行审核 。

5. 审核通过后你会获得一个生产环境的ClientID和secret,这一步你已经完成了PayPal付款。

创建Android 项目

1.创建一个Android项目并加入jar包:

jar下载地址:https://github.com/paypal/PayPal-Android-SDK/blob/master/libs/PayPalAndroidSDK-2.4.0.jar

<!– for card.io card scanning –><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.VIBRATE" /><uses-feature android:name="android.hardware.camera" android:required="false" /><uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /><!– for most things, including card.io & paypal –><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.INTERNET"/>

<service android:name="com.paypal.android.sdk.payments.PayPalService" android:exported="false" /><activity android:name="com.paypal.android.sdk.payments.PaymentActivity" /><activity android:name="com.paypal.android.sdk.payments.LoginActivity" /><activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" /><activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" /><activity android:name="io.card.payment.CardIOActivity" android:configChanges="keyboardHidden|orientation" /><activity android:name="io.card.payment.DataEntryActivity" />private static PayPalConfiguration config = new PayPalConfiguration()// 沙盒测试(ENVIRONMENT_SANDBOX),生产环境(ENVIRONMENT_PRODUCTION).environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)//你创建的测试应用Client ID.clientId("<YOUR_CLIENT_ID>");

5.在Activity onCreate和onDestroy方法,启动和停止PayPalService服务:

@Overrideprotected 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);}@Overridepublic void onDestroy() {stopService(new Intent(this, PayPalService.class));super.onDestroy();}

6.创建PayPalPayment intent,当点击按钮进入支付Activity:

public void onBuyPressed(View pressed) {/** PAYMENT_INTENT_SALE will cause the payment to complete immediately.* Change PAYMENT_INTENT_SALE to PAYMENT_INTENT_AUTHORIZE to only authorize payment and* capture funds later.* Also, to include additional payment details and an item list, see getStuffToBuy() below.*/PayPalPayment payment = new PayPalPayment(new BigDecimal("1.75"), "USD", "hipster jeans",PayPalPayment.PAYMENT_INTENT_SALE);Intent intent = new Intent(this, PaymentActivity.class);intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);startActivityForResult(intent, 0);}7.实现onActivityResult():@Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode == Activity.RESULT_OK) {PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);if (confirm != null) {try {Log.i("paymentExample", confirm.toJSONObject().toString(4));// TODO: 发送支付ID到你的服务器进行验证} 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支付功能,不过在接下来最后一步,验证Paypal付款详情,确保你的帐户收到实际的付款金额。

当完成支付后,PayPal会返回付款详情:

{

为支付ID, 在onActivityResult方法发送支付ID到你的服务器端。

2.在服务器端调用PayPal API ,通过支付ID值来获取付款详情并验证。

如果你在以的话,别人就会知道你害怕被说,他们就会加倍地说你,

PayPal Android SDK 2.0 支付

相关文章:

你感兴趣的文章:

标签云: