android移动支付——PayPal支付

简介: android移动支付——PayPal支付

前言

 

这里开篇讲解一系列的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 ID

private 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有几种类型如图:

image.png

image.png

image.png

image.png


相关文章
|
移动开发 Java 开发工具
Android客户端三步完成支付宝支付SDK接入
Android客户端三步完成支付宝支付SDK接入
1899 0
|
7月前
|
小程序 Android开发 iOS开发
微信小程序-虚拟支付:适用场景 / iPhone调试用支付成功,Android调用失败,提示“小程序支付能力已被限制” / “errMsg“.“requestPayment:fail banned”
微信小程序-虚拟支付:适用场景 / iPhone调试用支付成功,Android调用失败,提示“小程序支付能力已被限制” / “errMsg“.“requestPayment:fail banned”
243 0
|
监控 前端开发 Java
Android自定义控件(十)——SurfaceView实战实现天气APP背景移动效果
Android自定义控件(十)——SurfaceView实战实现天气APP背景移动效果
355 0
|
XML 存储 前端开发
Android自定义控件(二)——支付宝支付成功动画
读律看书三九年,乌纱头上有青天,男儿欲画凌烟阁,第一功名不爱钱。
233 0
Android自定义控件(二)——支付宝支付成功动画
|
Java Android开发
移动应用程序设计基础——Android环境构建与Activity生命周期
安装智能手机开发相关软件平台,并在此基础上测试Activity的生命周期过程。 5、 完成智能手机开发平台安装、以及相关配置; 6、 并实现Hello World; 7、 添加Log日志,通过Log日志验证Ac 1、 安装JAVA JDK 2、 安装Android Studio,熟悉AS的基本操作,改变AS的字体,显示方式;截图和文字说明。 3、 建立新项目,实现Hello World。说明各个文件的作用,以及各个关键语句的作用或含义,给出程序的运行结果。 4、 设置生命周期的Log日志,分别执行相关操作
233 0
移动应用程序设计基础——Android环境构建与Activity生命周期
|
Java 开发工具 Android开发
Android 银联控件支付开发流程
项目中要用到支付功能,需要支付宝支付、微信支付、银联支付,所以打算总结一下,方便以后的查阅,也方便大家, 用到的地方避免再次被坑。 今天我们就主要介绍一下银联控件支付,其他支付也写了对应教程,并且给出了连接。
193 0
|
Java 开发工具 Android开发
Android 支付宝支付开发流程
项目中要用到支付功能,需要支付宝支付、微信支付、银联支付,所以打算总结一下,方便以后的查阅,也方便大家, 用到的地方避免再次被坑。 今天我们就主要介绍一下支付宝支付,其他支付也给出了对应的连接。
472 0
|
测试技术 API 数据安全/隐私保护
Android——实现人脸支付
功能实现 人脸支付 API初始化 人脸特征提取 返回支付结果 密码框输入支付 自定义密码输入框控件 初始化控件 密码匹配 尾言 效果展
305 0
|
缓存 安全 Java
android移动支付——微信支付
android移动支付——微信支付
|
Android开发
Android-移动支付 支付宝 微信 银联
Android-移动支付 支付宝 微信 银联