RxPay快速集成微信、支付宝支付。

1,857 阅读1分钟
原文链接: github.com

本项目实现支付宝、微信支付。

基于 Vinctor/RxPay 修改实现,感谢原作者。有如下不同:

  • 支持RxJava2。

  • 对支付结果的二次封装。

RxPay的优点:

  • 使用简单、清晰。

  • 不需要编写WXPayEntryActivity类以及声明微信要求的广播类AppRegister

使用方式

添加依赖

    compile ('com.github.simplezhli.RxPay:pay-api:v1.0.5'){
        exclude module: 'appcompat-v7'
    }
    annotationProcessor 'com.github.simplezhli.RxPay:pay-compiler:v1.0.5'

支付宝

   RxAliPay.getIntance()
           .with(MainActivity.this, sign) //服务器端返回签名
           .requestPay()
           .observeOn(AndroidSchedulers.mainThread())
           .subscribe(new Observer<PayResult>() {
                 @Override
                 public void onSubscribe(Disposable d) {}
   
                 @Override
                 public void onNext(PayResult payResult) {
                       Toast.makeText(MainActivity.this, "支付成功!" , Toast.LENGTH_SHORT).show();
                 }
   
                 @Override
                 public void onError(Throwable e) {
                       Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                 }
   
                 @Override
                       public void onComplete() {}
                 });        
           

微信

1.在你自定义的Application中初始化

   RxWxPay.init(this);

2.对你任意一个activity类进行如下注解:

   @WXPay(BuildConfig.APPLICATION_ID)
   public class MainActivity extends AppCompatActivity {
   }

之后Make Project

3.AndroidManifest.xml加入

    <activity
          android:name=".wxapi.WXPayEntryActivity"
          android:exported="true"
          android:launchMode="singleTop"
          android:screenOrientation="portrait" />

4.调用方法

   RxWxPay.WXPayBean payBean = new RxWxPay.WXPayBean("appid", "partnerid", "noncestr",
           "timestamp", "prepayid", "sign");
   
   RxWxPay.getIntance()
          .withWxPayBean(payBean)
          .requestPay()
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(new Observer<WxPayResult>() {
                 @Override
                 public void onSubscribe(Disposable d) {}
   
                 @Override
                 public void onNext(WxPayResult wxPayResult) {
                       Toast.makeText(MainActivity.this, "支付成功!" , Toast.LENGTH_SHORT).show();
                 }
   
                 @Override
                 public void onError(Throwable e) {
                        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                 }
   
                 @Override
                       public void onComplete() {}
                 });

License

Copyright 2017 simplezhli

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.