前言

这里开篇讲解一系列的Android相关的移动支付。移动支付也称为手机支付,用户使用移动的设备,完成对所购买商品或者服务的支付功能。包括远程支付(网上支付、短信支付),近场支付(刷卡、滴卡、pos机)。

国内的移动支付方式:

支付宝,微信,银联,百度钱包,QQ钱包,财付通,京东支付

易宝支付,快钱支付,还有一些第三方的支付什么连连支付之类的

境外的移动支付方式(这里直说一个) :paypal

流程

我们这里主要是来学习一下支付宝,微信支付,银联支付和paypal支付

现实生活中的支付就是:去商店浏览商品->把商品加入购物车->把购物车中的商品拿到收银台付款

上面的支付流程细化下来就是:

1.浏览商品

2.把要买的商品加入购物车

3.把商品拿到收银台,收银人员处理商品信息

4.告诉收银员支付方式

5.选择支付方式进行支付

6.处理支付结果(成功、失败、取消)

程序中的支付流程中:

1.浏览商品

2.把要买的商品加入购物车

3.把购物车中的商品信息和用户信息和支付方式等信息发送到自己服务器,服务器处理商品信息生成订单,并返回”支付串”给客户端

4.客户端拿着“支付串”,调用第三方服务(支付宝、微信、银联、paypal等)完成支付

5.处理支付结果(成功、失败、取消)

  l  同步返回:支付后通知我们的客户端

  l  异步通知:支付后通知我们的服务端

以上就是一般的移动支付的基本流程了,下面看这几个支付平台的详细介绍

android移动支付——支付宝支付

android移动支付——微信支付

android移动支付——银联支付

android移动支付——PayPal支付


PayPal支付

上面的支付方式都是中国境内常用的支付方式,那如果想用境外的支付呢,无疑Paypal是一个不错的选择,下面来接入它:

1.       注册PayPal开发者账号,并且添加你的APP

首先我们要去PayPal的开发者平台注册账号,并且创建应用,获取Client ID

2.       可以在PayPal的github上看到它一些详细的介绍

可以拿到依赖,配置一下你的工程:

compile('com.paypal.sdk:paypal-android-sdk:2.15.1'){ excludegroup: 'io.card' }//禁止通过信用卡直接支付,如果不禁止可以直接去掉这一句

3.       集成的最低版本

由于PayPal默认集成的最低版本是minSdkVersion 16或者更高,所以你的AS编译版本低于这个版本的时候,AS会提示你编译不通过,报错等等,这是你需要在清单文件AndroidMainfest强制一下你需要的版本下编译:


4.       在你需要支付的页面配置支付环境(或者在你的基类里面配置)

这里可以到官网上申请商户和个人的沙盒测试账号进行测试

 

// 配置各种支付类型,一般就沙盒测试的和正式的privatestatic final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK;// note that these credentialswill differ between live & sandbox environments.// 这是在第一步时候注册创建得来的Client IDprivate static final String CONFIG_CLIENT_ID = "credentials from developer.paypal.com";private static final int REQUEST_CODE_PAYMENT = 1;private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;private static final int REQUEST_CODE_PROFILE_SHARING = 3;private static PayPalConfigurationconfig = new PayPalConfiguration()        .environment(CONFIG_ENVIRONMENT)        .clientId(CONFIG_CLIENT_ID)        // The following are only used inPayPalFuturePaymentActivity.// 下面的这些都是要用到授权支付才用到的,不用就注释掉可以了        .merchantName("Example Merchant")        .merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))        .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));

5.       在类的onCreate里面调起支付服务

Intentintent = new Intent(this, PayPalService.class);intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);startService(intent);

6.       在需要支付的地方调起支付功能,这里有两种类型

publicvoid onBuyPressed(View pressed) {    /*      * PAYMENT_INTENT_SALE will cause thepayment to complete immediately.     * Change PAYMENT_INTENT_SALE to      *  - PAYMENT_INTENT_AUTHORIZE to only authorize payment and capture fundslater.     *  - PAYMENT_INTENT_ORDER to create a payment for authorization and capture     *    later via calls from your server.     *      * Also, to include additionalpayment details and an item list, see getStuffToBuy() below.     */    PayPalPayment thingToBuy =getStuffToBuy(PayPalPayment.PAYMENT_INTENT_SALE);    /*     * See getStuffToBuy(..) for examplesof some available payment options.     */    Intent intent = new Intent(SampleActivity.this, PaymentActivity.class);    // send the same configuration for restartresiliency    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);    startActivityForResult(intent, REQUEST_CODE_PAYMENT);}private PayPalPayment getThingToBuy(String paymentIntent) {    return new PayPalPayment(new BigDecimal("0.01"), "USD", "sample item",            paymentIntent);}/*  * This method shows use of optionalpayment details and item list. */private PayPalPayment getStuffToBuy(String paymentIntent) {    //--- include an item list, payment amountdetails    PayPalItem[] items =        {                new PayPalItem("sample item #1", 2, new BigDecimal("87.50"), "USD",                        "sku-12345678"),                new PayPalItem("free sample item #2", 1, new BigDecimal("0.00"),                        "USD", "sku-zero-price"),                new PayPalItem("sample item #3 with a longername", 6, new BigDecimal("37.99"),                        "USD", "sku-33333")         };    BigDecimal subtotal = PayPalItem.getItemTotal(items);    BigDecimal shipping = new BigDecimal("7.21");    BigDecimal tax = new BigDecimal("4.67");    PayPalPaymentDetails paymentDetails = new PayPalPaymentDetails(shipping, subtotal, tax);    BigDecimal amount =subtotal.add(shipping).add(tax);    PayPalPayment payment= new PayPalPayment(amount, "USD", "sample item", paymentIntent);    payment.items(items).paymentDetails(paymentDetails);    //--- set other optional fields likeinvoice_number, custom field, and soft_descriptor    payment.custom("This is text that will be associatedwith the payment that the app can use.");    return payment;}

7.       然后类的onActivityResult 里进行回调结果的处理

 

if (resultCode == Activity.RESULT_OK) {    PaymentConfirmation confirm =           data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);    if (confirm != null) {        try {            Log.i(TAG, confirm.toJSONObject().toString(4));            Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));            /**             *  TODO: send 'confirm' (and possiblyconfirm.getPayment() to your server for verification             * or consent completion.             * Seehttps://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/             * for more details.             *             * For sample mobile backendinteractions, see             *https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend             */            displayResultText("PaymentConfirmation info receivedfrom PayPal");        } catch (JSONException e) {            Log.e(TAG, "an extremely unlikely failure occurred: ", e);        }    }} else if (resultCode == Activity.RESULT_CANCELED) {    Log.i(TAG, "The user canceled.");} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {    Log.i(            TAG,            "An invalid Payment or PayPalConfiguration wassubmitted. Please see the docs.");}

这里授权支付部分就不贴出来了,在github上面的Smaple有代码看,具体可以看一下

https://github.com/paypal/PayPal-Android-SDK

同时支持Kotlin语言

 

8.       在onDestroy 注销服务

stopService(new Intent(this, PayPalService.class));

9.运行demo有几种类型如图:





 



更多相关文章

  1. Android(安卓)多平台解决方案
  2. Android(安卓)WebView漏洞
  3. [视频]Firefox for Android(安卓)预览版放出
  4. Android(安卓)Studio中调用高于minSdkVersion版本API, 无提示 (L
  5. Androids中的System.loadLibrary对于依赖so的加载分析
  6. Android(安卓)Git 常用命令和规范
  7. android 6.0的变化
  8. Android(安卓)targetSDK升级到28,gradle升级4.6遇到的问题
  9. Bitmaps加载之内存管理

随机推荐

  1. Android(安卓)Wi-Fi Disable Roaming in
  2. Android屏幕休眠禁止或清除
  3. Android根据电量变化为不同图片的方法【
  4. android文件存取
  5. android EditText 监听复制粘贴等操作
  6. Android之Style
  7. Android——通过电话号码获得联系人图像
  8. How to construct a Android(安卓)projec
  9. Android(安卓)R
  10. 模仿Android(安卓)location provider