Skip to content

Commit

Permalink
支付宝新增 登录功能 同时对回调函数进行修改
Browse files Browse the repository at this point in the history
  • Loading branch information
RelinRan committed Nov 7, 2019
1 parent 60ab7ce commit c7b67f4
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 89 deletions.
210 changes: 210 additions & 0 deletions app/src/main/java/com/android/pay/alipay/AliLogin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package com.android.pay.alipay;

import android.app.Activity;
import android.os.Bundle;

import com.alipay.sdk.app.OpenAuthTask;

import java.util.HashMap;
import java.util.Map;

public class AliLogin implements OpenAuthTask.Callback {

/**
* 调用成功
*/
public static final int OK = OpenAuthTask.OK;

/**
* 3s 内快速发起了多次支付 / 授权调用。稍后重试即可。
*/
public static final int DUPLEX = OpenAuthTask.Duplex;

/**
* 用户未安装支付宝 App
*/
public static final int NOT_INSTALLED = OpenAuthTask.NOT_INSTALLED;

/**
* 其它错误,如参数传递错误
*/
public static final int SYS_ERR = OpenAuthTask.SYS_ERR;

/**
* 登录页面
*/
public final Activity activity;

/**
* 支付宝平台APP_ID
*/
public final String appId;

/**
* 支付宝回跳到你的应用时使用的 Intent Scheme。请设置为不和其它应用冲突的值,需要在你的 App 的 AndroidManifest.xml 中添加这一项
*/
public final String scheme;

/**
* 登录回调监听
*/
public final OnAliLoginListener listener;


/**
* 登录构造函数
*
* @param builder
*/
public AliLogin(Builder builder) {
this.activity = builder.activity;
this.scheme = builder.scheme;
this.appId = builder.appId;
this.listener = builder.listener;
login();
}


public static class Builder {

/**
* 登录构建者
*
* @param activity 登录页面
*/
public Builder(Activity activity) {
this.activity = activity;
}

/**
* 登录页面
*/
private Activity activity;

/**
* 支付宝平台ID
*/
private String appId;

/**
* 支付宝回跳到你的应用时使用的 Intent Scheme。请设置为不和其它应用冲突的值。
*/
private String scheme;

/**
* 登录监听
*/
private OnAliLoginListener listener;

/**
* 获取当前登录页面
*
* @return
*/
public Activity activity() {
return activity;
}

/**
* 平台ID
*
* @return
*/
public String appId() {
return appId;
}

/**
* 设置平台ID
*
* @param appId
* @return
*/
public Builder appId(String appId) {
this.appId = appId;
return this;
}

/**
* 支付宝回跳到你的应用时使用的 Intent Scheme
*
* @return
*/
public String scheme() {
return appId;
}

/**
* 设置支付宝回跳到你的应用时使用的 Intent Scheme,需要在你的 App 的 AndroidManifest.xml 中添加这一项
*
* @param scheme
* @return
*/
public Builder scheme(String scheme) {
this.scheme = scheme;
return this;
}

/**
* 获取登录监听
*
* @return
*/
public OnAliLoginListener listener() {
return listener;
}

/**
* 设置登录监听
*
* @param listener
* @return
*/
public Builder listener(OnAliLoginListener listener) {
this.listener = listener;
return this;
}

/**
* 构建登录对象
*
* @return
*/
public AliLogin build() {
return new AliLogin(this);
}
}

/**
* 登录
*/
private void login() {
// 传递给支付宝应用的业务参数
Map<String, String> bizParams = new HashMap<>();
bizParams.put("url", "https://authweb.alipay.com/auth?auth_type=PURE_OAUTH_SDK&app_id=" + appId + "&scope=auth_user&state=init");
// 唤起授权业务
OpenAuthTask task = new OpenAuthTask(activity);
task.execute(scheme, OpenAuthTask.BizType.AccountAuth, bizParams, this, true);
}

/**
* 登录结果
*
* @param resultCode
* @param memo
* @param bundle
*/
@Override
public void onResult(int resultCode, String memo, Bundle bundle) {
if (listener != null) {
AliUser user = new AliUser();
user.setAppId(bundle.getString("app_id"));
user.setResultCode(bundle.getString("result_code"));
user.setScope(bundle.getString("scope"));
user.setState(bundle.getString("state"));
user.setAuthCode(bundle.getString("auth_code"));
listener.onAliLogin(resultCode, memo, user);
}
}

}
65 changes: 65 additions & 0 deletions app/src/main/java/com/android/pay/alipay/AliUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.android.pay.alipay;

public class AliUser {

/**
* 支付宝分配给开发者的应用 ID
*/
private String appId;
/**
* 结果码
*/
private String resultCode;
/**
* 极简版 SDK 固定参数,传其他参数无效。auth_base 为用户基础授权,仅用于静默获取用户支付宝 UID;auth_user 获取用户信息,网站支付宝登录。
*/
private String scope;
/**
* OAuth 2 协议参数,可设置为随机字符串的 base64 编码(100位以内)
*/
private String state;
/**
* 授权码
*/
private String authCode;

public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public String getResultCode() {
return resultCode;
}

public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}

public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getAuthCode() {
return authCode;
}

public void setAuthCode(String authCode) {
this.authCode = authCode;
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/android/pay/alipay/OnAliLoginListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.android.pay.alipay;

public interface OnAliLoginListener {

/**
* 支付宝登录回调
*
* @param resultCode 例如{@link AliLogin#OK}判断用户登录成功
* @param memo 提示信息
* @param user 用户信息
*/
void onAliLogin(int resultCode, String memo, AliUser user);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.android.pay.wxlogin;
package com.android.pay.wechat;

/**
* 微信登录回调
*/
public interface OnWXLoginListener {
public interface OnWeChatLoginListener {


/**
Expand All @@ -13,6 +13,6 @@ public interface OnWXLoginListener {
* @param msg
* @param user
*/
void onWXLogin(int code, String msg, WXUser user);
void onWeChatLogin(int code, String msg, WeChatUser user);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.android.pay.wechat;

public interface OnWeChatPayListener {

void onWeChatPay(int code, String msg);

}
Loading

0 comments on commit c7b67f4

Please sign in to comment.