个人支付功能实现

个人支付功能实现

简单代码地址

  • 效果
    个人支付结果示意图
  • 前置条件
  1. android手机一部
  2. 安装payself(备注允许后台运行,该APP获取所有推送相关特权—注意部分手机可能设置比较蛋疼,或者干脆不支持,悉知。。。)
  3. 支付宝、微信(备注如上)
  4. 服务器一台

开发

android端 安装APP:payself

核心代码

/**
 * 作者@有点凉了
 * 欢迎加Q群讨论:950066277 群已删除
 */

public class NotificationMonitor extends NotificationListenerService {
    public ApiInterface request_interface = null;
    public HttpLoggingInterceptor loggingInterceptor = null;
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Bundle bundle = sbn.getNotification().extras;
        if (sbn.getPackageName().equals("com.eg.android.AlipayGphone")){//当前仅实现支付宝功能、微信功能同理
            if (request_interface==null){
                request_interface = ServerUrl.getApiInterface();
                loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        //打印retrofit日志
                        Log.e("RetrofitLog", "retrofitBack = " + message);
                    }
                });
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            }
            String content = bundle.getString("android.text");
            if (!TextUtils.isEmpty(content)){
                String [] contents = content.split("通过扫码向你付款");
                Log.e("==--> " ,contents[0]+"支付成功了"+contents[1]);
                String priceStr = contents[1].replace("元","");
                double price = Double.parseDouble(priceStr);
                Request(contents[0],price,sbn.getPackageName());

            }
        }
//        for (String key: bundle.keySet())
//        {
//            Log.e("Bundle Content", "Key=" + key + ", content=" +bundle.getString(key));
//        }

    }

    private void Request(String username,double price,String packageName) {
        //POST
        Call<PayResult> postCallPay = request_interface.postCallPay(username,price,packageName);
        postCallPay.enqueue(new Callback<PayResult>() {
            @Override
            public void onResponse(Call<PayResult> call, Response<PayResult> response) {
                try {
                    Log.e("==-->支付成功刷新结果:",response.body().getMsg());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                call.cancel();
            }

            @Override
            public void onFailure(Call<PayResult> call, Throwable t) {
                Log.e("==-->失败",t.getMessage());
            }
        });
    }

服务端(备注,环境需提前备好,数据库创建完毕)
创建订单状态表

CREATE TABLE `goodorder` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `orderId` varchar(40) NOT NULL COMMENT '订单id:sha1(username+buygoods+price+buygoods+当前时间戳)',
  `username` varchar(100) NOT NULL COMMENT '联系人',
  `payway` int(1) NOT NULL COMMENT '支付方式:目前先弄支付宝 1、支付宝 ;2 、微信',
  `price` decimal(10,2) NOT NULL COMMENT '支付金额',
  `orderstatus` int(1) NOT NULL COMMENT '0:未支付 ; 1 已支付',
  `buygoods` varchar(100) NOT NULL COMMENT '所购买商品',
  `create_time` int(10) NOT NULL,
  `update_time` int(10) NOT NULL,
  `delete_time` int(10) NOT NULL,
  `goodsId` int(5) NOT NULL COMMENT '商品id',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

    /**
     * 刷新订单状态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateGoods(){
        $username = Request::param("username");
        $price = Request::param("price");
        $packageName = Request::param("packageName");
        if ($packageName == "com.eg.android.AlipayGphone"){
            $payway = 1;
        }
        $goodorder = Goodorder::where('username',$username)
            ->where('orderstatus',0)
            ->where('price',$price)
            ->order('create_time','desc')
            ->limit(1)
            ->find();
        if (empty($goodorder)){
            return json(['code'=>1,'msg'=>'失败']);
        }
        $goodorder->orderstatus=1;
        $goodorder->payway=$payway;
        $goodorder->save();
        return json(['code'=>0,'msg'=>'成功']);
    }

具体可加群共同讨论。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
老规矩,先看本节效果图我们实现这个支付功能完全是借助小程序云开发实现的,不用搭建自己的服务器,不用买域名,不用备案域名,不用支持https。只需要一个简单的云函数,就可以轻松的实现微信小程序支付功能。核心代码就下面这些一,创建一个云开发小程序关于如何创建云开发小程序,这里我就不再做具体讲解。不知道怎么创建云开发小程序的同学,可以去翻看我之前的文章,或者看下我录制的视频:https://edu.csdn.net/course/play/9604/204528创建云开发小程序有几点注意的1,一定不要忘记在app.js里初始化云开发环境。2,创建完云函数后,一定要记得上传二, 创建支付的云函数1,创建云函数pay三,引入三方依赖tenpay我们这里引入三方依赖的目的,是创建我们支付时需要的一些参数。我们安装依赖是使用里npm 而npm必须安装node,关于如何安装node,我这里不做讲解,百度一下,网上一大堆。1,首先右键pay,然后选择在终端中打开2,我们使用npm来安装这个依赖。在命令行里执行 npm i tenpay安装完成后,我们的pay云函数会多出一个package.json 文件到这里我们的tenpay依赖就安装好了。四,编写云函数pay完整代码如下//云开发实现支付 const cloud = require('wx-server-sdk')cloud.init() //1,引入支付的三方依赖 const tenpay = require('tenpay'); //2,配置支付信息 const config = ;exports.main = async(event, context) => 一定要注意把appid,mchid,partnerKey换成你自己的。到这里我们获取小程序支付所需参数的云函数代码就编写完成了。不要忘记上传这个云函数。出现下图就代表上传成功五,写一个简单的页面,用来提交订单,调用pay云函数。这个页面很简单,1,自己随便编写一个订单号(这个订单号要大于6位)2,自己随便填写一个订单价(单位是分)3,点击按钮,调用pay云函数。获取支付所需参数。下图是官方支付api所需要的一些必须参数。下图是我们调用pay云函数获取的参数,和上图所需要的是不是一样。六,调用wx.requestPayment实现支付下图是官方的示例代码这里不在做具体讲解了,完整的可以看视频。实现效果1,调起支付键盘2,支付完成3,log日志,可以看出不同支付状态的回调上图是支付成功的回调,我们可以在支付成功回调时,改变订单支付状态。下图是支付失败的回调,下图是支付完成的状态。到这里我们就轻松的实现微信小程序的支付功能了。是不是很简单啊,完整的讲解可以看视频。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时有晌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值