Paypal支付
Posted Ashan-Da宝旗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Paypal支付相关的知识,希望对你有一定的参考价值。
立即支付
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 2.在 AndroidManifest.xml 文件添加如下权限: [html] view plain copy- <!-- 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"/>
3.在 AndroidManifest.xml 文件,标签: <application> 内添加 service和activities :
[html] view plain copy
- <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" />
4.new PayPalConfiguration
实例并初始化配置:
[java] view plain copy
- 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
服务:
[java] view plain copy
- @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
- public void onDestroy()
- stopService(new Intent(this, PayPalService.class));
- super.onDestroy();
6.创建PayPalPayment intent,当点击按钮进入支付Activity:
[java] view plain copy
- 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()
:
[java] view plain copy
- @Override
- protected 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);
- 经典 API NVP 交易详情调用中未抓取来自 PayPal 按钮支付的文本字段数据