Android-推荐支付集成库(Android-Pay)

图片来源网络,入侵必删

Android的日常开发中,我们肯定会遇到支付需求的实现,一般情况,都会要求我们集成支付宝支付微信支付。这里分享一个开源库库,帮你快速实现支付功能。

Android-Pay

一个帮你快速实现微信支付支付宝支付的开源库。开源库的wiki,可以查看源码

导入项目

dependencies {
    implementation 'io.github.mayubao:pay_library:1.0.1'
}

截止我发博客的时候,最近更新的版本是1.0.1。如果想要看有没有更新,请看开源库的wiki。

简单使用

微信支付:

        //1.create request for wechat pay
        WechatPayReq wechatPayReq = new WechatPayReq.Builder()
                .with(this) //activity instance
                .setAppId(appid) //wechat pay AppID
                .setPartnerId(partnerid)//wechat pay partner id
                .setPrepayId(prepayid)//pre pay id
//                              .setPackageValue(wechatPayReq.get)//"Sign=WXPay"
                .setNonceStr(noncestr)
                .setTimeStamp(timestamp)//time stamp
                .setSign(sign)//sign
                .create();
        //2. send the request with wechat pay
        PayAPI.getInstance().sendPayRequest(wechatPayReq);


        //set the callback for wechat pay
        //wechatPayReq.setOnWechatPayListener(new OnWechatPayListener);

支付宝支付:

        //step 1 create raw ali pay order info
        String rawAliOrderInfo = new AliPayReq2.AliOrderInfo()
                .setPartner(partner) //set partner
                .setSeller(seller)  //set partner seller accout
                .setOutTradeNo(outTradeNo) //set unique trade no
                .setSubject(orderSubject) //set order subject
                .setBody(orderBody) //set order detail
                .setPrice(price) //set price
                .setCallbackUrl(callbackUrl) //set callback for pay reqest
                .createOrderInfo(); //create ali pay order info


        //step 2 get the signed ali pay order info
        String signAliOrderInfo = getSignAliOrderInfoFromServer(rawAliOrderInfo);

        //step 3 step 3 send the request for ali pay
        AliPayReq2 aliPayReq = new AliPayReq2.Builder()
                .with(activity)//Activity instance
                .setSignedAliPayOrderInfo(signAliOrderInfo)
                .setRawAliPayOrderInfo(rawAliOrderInfo)//set the ali pay order info
                .setSignedAliPayOrderInfo(signAliOrderInfo) //set the signed ali pay order info
                .create()//
                .setOnAliPayListener(null);//
        PayAPI.getInstance().sendPayRequest(aliPayReq);


        //set the ali pay callback
        //aliPayReq.setOnAliPayListener(new OnAliPayListener);

常见的问题

new folder named 'wxapi' under your project root package

在wxapi下创建类WXPayEntryActivity

实现IWXAPIEventHandler接口

回调类的样板:


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{

   private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
   
   private IWXAPI api;
   
   //TODO need to replace your APP_ID
   private String APP_ID = "wx0xxxxxxxxx"; //need to replace your APP_ID
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
       
    api = WXAPIFactory.createWXAPI(this, APP_ID);
       api.handleIntent(getIntent(), this);
   }

   @Override
   protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
       api.handleIntent(intent, this);
   }

   @Override
   public void onReq(BaseReq req) {
   }

   @Override
   public void onResp(BaseResp resp) {
    Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

    if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//          AlertDialog.Builder builder = new AlertDialog.Builder(this);
//          builder.setTitle(R.string.app_tip);
//          builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//          builder.show();

           //send the wechat pay broadcast receiver,and you should the define wechat pay broadcast receive 

        Intent intent = new Intent();
        intent.setAction(WeChatPayReceiver.ACTION_PAY_RESULT);
        intent.putExtra("result", resp.errCode);
        sendBroadcast(intent);
        
        finish();
    }
   }
}

混淆代码

#pay_library
-dontwarn io.github.mayubao.pay_library.**
-keep class io.github.mayubao.pay_library.** {*;}

#wechat pay
-dontwarn com.tencent.**
-keep class com.tencent.** {*;}


#alipay
-dontwarn com.alipay.**
-keep class com.alipay.** {*;}

-dontwarn  com.ta.utdid2.**
-keep class com.ta.utdid2.** {*;}

-dontwarn  com.ut.device.**
-keep class com.ut.device.** {*;}

-dontwarn  org.json.alipay.**
-keep class corg.json.alipay.** {*;}

使用这个库会比原生的接入简单一点,初学的小伙伴可以看看这个库的源码学习一下对支付的封装。

你可能感兴趣的:(Android-推荐支付集成库(Android-Pay))