PayPal SDK 2.0支持两种支付方式:立即支付和后支付,下面介绍的是立即支付方式。
1.用户使用PayPal账户或信用卡支付,支付完成接收返回一个支付ID。
2.在你的服务器端,使用PayPal的API获取付款详情,进行支付验证。
4.new PayPalConfiguration
实例并初始化配置:
private static PayPalConfiguration config = new PayPalConfiguration()
// 沙盒测试(ENVIRONMENT_SANDBOX),生产环境(ENVIRONMENT_PRODUCTION)
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
//你创建的测试应用Client ID
.clientId("");
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;
}