第三方支付支付宝支付

支付宝info

package com.itheima.alipaydemo26;

/**
 * 支付宝支付模型对象
 */
public class AlipayInfo {


    /**
     * payInfo :
     * errMsg : 请求成功
     * errCode : 0
     * payType : 1
     */

    private String payInfo; //支付串码
    private String errMsg;
    private String errCode;
    private String payType;

    public String getPayInfo() {
        return payInfo;
    }

    public void setPayInfo(String payInfo) {
        this.payInfo = payInfo;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    @Override
    public String toString() {
        return "AlipayInfo{" +
                "payInfo='" + payInfo + '\'' +
                ", errMsg='" + errMsg + '\'' +
                ", errCode='" + errCode + '\'' +
                ", payType='" + payType + '\'' +
                '}';
    }
}


核心代码

package com.itheima.alipaydemo26;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alipay.sdk.app.PayTask;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;


public class MainActivity extends AppCompatActivity implements Response.ErrorListener, Response.Listener<String> {

    private String url;
    private AlipayInfo alipayInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        url = "http://192.168.33.88:8080/HeiMaPay/Pay?goodId=111&count=1&price=0.01";//支付协议
    }

    public void alipay(View v){
        //支付四部曲
        //1.提交信息到服务器
        StringRequest request = new StringRequest(url, this, this);
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);

        //2.解析服务器返回的“支付串码”
        //3.调用第三方支付SDK的支付方法,传入“支付串码”
        //4.处理支付结果
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        showLog("error:"+volleyError.getMessage());
    }

    @Override
    public void onResponse(String s) {
        //2.解析服务器返回的“支付串码”
        alipayInfo = JSON.parseObject(s, AlipayInfo.class);
        showLog(alipayInfo.toString());


        new Thread(new Runnable() {
            @Override
            public void run() {
                //3.调用第三方支付SDK的支付方法,传入“支付串码”
                PayTask payTask = new PayTask(MainActivity.this);
                //同步返回支付结果
                String payResult = payTask.pay(alipayInfo.getPayInfo(), true);

                Message msg = mHandler.obtainMessage();
                msg.obj = payResult;
                mHandler.sendMessage(msg);
            }
        }).start();
    }

    private Handler mHandler = new MyHandler();


    class MyHandler extends  Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            PayResult payResult = new PayResult((String) msg.obj);
            showLog("payresult="+payResult);
            /**
             * 同步返回的结果必须放置到服务端进行验证(验证的规则请看https://doc.open.alipay.com/doc2/
             * detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665&
             * docType=1) 建议商户依赖异步通知
             */
            String resultInfo = payResult.getResult();// 同步返回需要验证的信息

            String resultStatus = payResult.getResultStatus();
            // 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档
            if (TextUtils.equals(resultStatus, "9000")) {
                showToast("支付成功");
                showLog("支付成功");
            } else {
                // 判断resultStatus 为非"9000"则代表可能支付失败
                // "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
                if (TextUtils.equals(resultStatus, "8000")) {
                    showToast("支付结果确认中");

                } else if(TextUtils.equals(resultStatus, "6001")){
                    showToast("取消失败");
                }else {
                    // 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
                    showToast("支付失败");
                }
            }
        }
    }

    private void showLog(String msg){
        Log.e("result", msg);
    }

    private void showToast(String msg){
        Toast.makeText(this, "支付结果(26期):" + msg, Toast.LENGTH_SHORT).show();
    }
}


payresule

package com.itheima.alipaydemo26;

import android.text.TextUtils;

/**
 * 解析支付宝支付返回结果:json
 */
public class PayResult {
	private String resultStatus;
	private String result;
	private String memo;

	public PayResult(String rawResult) {

		if (TextUtils.isEmpty(rawResult))
			return;

		String[] resultParams = rawResult.split(";");
		for (String resultParam : resultParams) {
			if (resultParam.startsWith("resultStatus")) {
				resultStatus = gatValue(resultParam, "resultStatus");
			}
			if (resultParam.startsWith("result")) {
				result = gatValue(resultParam, "result");
			}
			if (resultParam.startsWith("memo")) {
				memo = gatValue(resultParam, "memo");
			}
		}
	}

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

	private String gatValue(String content, String key) {
		String prefix = key + "={";
		return content.substring(content.indexOf(prefix) + prefix.length(),
				content.lastIndexOf("}"));
	}

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

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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值