PayPal 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包:
2.在
AndroidManifest.xml
文件添加如下权限:
<!-- 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
:
<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
实例并初始化配置:
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
服务:
@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:
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()
:
@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); } } } 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付款详情,确保你的帐户收到实际的付款金额。
1.在Android应用程序,发送支付ID到你的服务器(方法:onActivityResult())。
当完成支付后,PayPal会返回付款详情:
{
"client": {
"environment": "sandbox",
"paypal_sdk_version": "2.0.0",
"platform": "Android",
"product_name": "PayPal Andorid SDK;"
},
"response": {
"create_time": "2014-02-12T22:29:49Z",
"id": "PAY-564191241M8701234KL57LXI",
"intent": "sale",
"state": "approved"
},
"response_type": "payment"
}
//在response中 "id": "PAY-564191241M8701234KL57LXI"
为支付ID, 在onActivityResult方法发送支付ID到你的服务器端。
2.在服务器端调用PayPal API ,通过支付ID值来获取付款详情并验证。
如下代码:
public boolean verifyPayment(String paymentId){
//从PayPal获取付款详情
String str = getPaymentDetails(paymentId);
JSONObject detail = JSONObject.fromObject(str);
//校验订单是否完成
if("approved".equals(detail.optString("state"))){
JSONObject transactions = detail.optJSONArray("transactions").optJSONObject(0);
JSONObject amount = transactions.optJSONObject("amount");
JSONArray relatedResources = transactions.optJSONArray("related_resources");
//从数据库查询总金额与Paypal校验支付总金额
double total = 0;
if( total != amount.optDouble("total") ){
return false;
}
//校验交易货币类型
String currency = "USD";
if( !currency.equals(amount.optDouble("currency")) ){
return false;
}
//校验每个子订单是否完成
for (int i = 0,j = relatedResources.size(); i < j; i++) {
JSONObject sale = relatedResources.optJSONObject(i).optJSONObject("sale");
if( !"completed".equals(sale.optString("state")) ){
System.out.println("子订单未完成,订单状态:"+sale.optString("state"));
}
}
return true;
}
return false;
}
代码下载
该示例应用程序提供了完整的例子,下载:PayPal SDK Example