1. 程式人生 > >個人支付功能實現

個人支付功能實現

個人支付功能實現

  • 效果
    個人支付結果示意圖
  • 前置條件
  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'=>'成功']);
    }

具體可加群共同討論。