Android 集成支付宝支付

1.省略去开放平台创建应用和申请的过程

(此处省略N个字)

 

2.我们同样要去支付宝开放平台,获取我们需要的jar包

支付宝开放平台

 

3.导入完jar后,在清单文件中提前配置

<activity
    android:name="com.alipay.sdk.app.H5PayActivity"
    android:configChanges="orientation|keyboardHidden|navigation|screenSize"
    android:exported="false"
    android:screenOrientation="behind"
    android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
<activity
    android:name="com.alipay.sdk.app.H5AuthActivity"
    android:configChanges="orientation|keyboardHidden|navigation"
    android:exported="false"
    android:screenOrientation="behind"
    android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>

 

4.我们需要一个PayResult类(这个类不同于微信,不需要放在wxapi文件夹下) 

接下来上代码(可自提)

 

private String resultStatus;
private String result;
private String memo;

public PayResult(Map<String, String> rawResult) {
    if (rawResult == null) {
        return;
    }

    for (String key : rawResult.keySet()) {
        if (TextUtils.equals(key, "resultStatus")) {
            resultStatus = rawResult.get(key);
        } else if (TextUtils.equals(key, "result")) {
            result = rawResult.get(key);
        } else if (TextUtils.equals(key, "memo")) {
            memo = rawResult.get(key);
        }
    }
}

@Override
public String toString() {
    return "resultStatus={" + resultStatus + "};memo={" + memo
            + "};result={" + result + "}";
}

/**
 * @return the resultStatus
 */
public String getResultStatus() {
    return resultStatus;
}

/**
 * @return the memo
 */
public String getMemo() {
    return memo;
}

/**
 * @return the result
 */
public String getResult() {
    return result;
}

 

5.在这里呢 我直接在View层进行了下订单的操作

 

/**
 *
 * 支付宝支付
 *
 *
 */

String path = "下订单的接口 写上即可";
OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(5000, TimeUnit.SECONDS)
        .writeTimeout(5000, TimeUnit.SECONDS)
        .readTimeout(5000, TimeUnit.SECONDS)
        .build();

//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
builder.add("参数1", "*");
builder.add("参数2", *);

Request request = new Request.Builder()
        .url(path)
        .addHeader("头信息1", *******)
        .addHeader("头信息2", *******)
        .addHeader("头信息3", *******)
        .addHeader("头信息4", *******)
        .post(builder.build())
        .build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
    public static final int ALI_PAY_OK = 0;

    @Override
    public void onFailure(Call call, IOException e) {
        Log.i("aaa", "PayActivity onFailure:" + e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        // 获取必须来自服务端 预支付结果订单信息
        String signResult = response.body().string();
        Log.i("aaa", "PayActivity onResponse:" + signResult.toString());
        // 构造PayTask 支付宝对象
        PayTask alipay = new PayTask(WxinAndZFBPayActivity.this);
        // 调用支付接口,获取支付结果
        String result = alipay.pay(signResult, true);

        Message msg = new Message();
	//ALI_PAY_OK 发送消息的标志 接下来的Handler 要用 我这里写的是0
        msg.what = ALI_PAY_OK;		
        msg.obj = result;
        mHandler.sendMessage(msg);

6.因为我们在上面进行了网络请求,所以需要Handler发送消息 更新UI(自提)

 

//支付宝返回数据handler
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0:
                PayResult payResult = new PayResult((Map<String, String>) msg.obj);

                //同步获取结果
                String resultInfo = payResult.getResult();
                Log.i("Pay", "Pay:" + resultInfo);
                String resultStatus = payResult.getResultStatus();
                // 判断resultStatus 为9000则代表支付成功
                if (TextUtils.equals(resultStatus, "9000")) {
                    Toast.makeText(WxinAndZFBPayActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(WxinAndZFBPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
};

 

到这里呢 支付宝支付也就完成了 是不是也非常简单呢?

 

想了解更多有趣的博文?

          今天推荐的是:

           Android 集成微信支付(无修改版)

                                           https://blog.csdn.net/as89751/article/details/82456160

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

阅读更多
换一批

没有更多推荐了,返回首页