hypref框架yurunsoft/PaySDK的使用教程与源代码

您所在的位置:网站首页 汉语拼音字母表读法及视频 hypref框架yurunsoft/PaySDK的使用教程与源代码

hypref框架yurunsoft/PaySDK的使用教程与源代码

#hypref框架yurunsoft/PaySDK的使用教程与源代码| 来源: 网络整理| 查看: 265

1,在linux服务器端,用composer安装yurunsoft/PaySDK

操作命令(谁让我是个好人呢):(1)定义到根目录,cd /www/wwwroot/#####/hyperf-skeleton(2)composer require yurunsoft/pay-sdk=3.0

2.安装完成后,hyperf代码

(微信支付与回调) //微信支付 public function wxpay(RequestInterface $request,ResponseInterface $response) { $order_sn = $request->input('order_sn',''); $data = Db::table('order')->where('order_sn',$order_sn)->first(); $payment_all = Db::table('payments')->where('id',4)->first(); $payment = json_decode($payment_all['config'],true); if(!empty($payment) && !empty($data)) { if(empty($payment['appID'])) { $result['code'] = -1; $result['msg'] = 'appID未填写,请先完善资料!'; return json_encode($result); } if(empty($payment['mch_id'])) { $result['code'] = -1; $result['msg'] = '微信支付商户号未填写,请先完善资料!'; return json_encode($result); } if(empty($payment['key'])) { $result['code'] = -1; $result['msg'] = '微信支付密钥未填写,请先完善资料!'; return json_encode($result); } $params = new \Yurun\PaySDK\Weixin\Params\PublicParams(); $params->appID = $payment['appID']; $params->mch_id = $payment['mch_id']; $params->sign_type = 'MD5'; $params->key = $payment['key']; if(isset($payment['api_cert']) && !empty($payment['api_cert'])) { $params->certPath = BASE_PATH.'/public'.$payment['api_cert']; } if(isset($payment['api_key']) && !empty($payment['api_key'])) { $params->keyPath = BASE_PATH.'/public'.$payment['api_key']; } // SDK实例化,传入公共配置 $pay = new \Yurun\PaySDK\Weixin\SDK($params); $remote_addr_info = $request->getServerParams(); $pay->swooleRequest = $request; $pay->swooleResponse = $response; // 支付接口 $request = new \Yurun\PaySDK\Weixin\Native\Params\Pay\Request(); $request->body = $data['order_name']; // 商品描述 $request->out_trade_no = $data['order_sn']; // 订单号 $request->total_fee = doubleval($data['order_price']) * 100; // 订单总金额,单位为:分 $request->spbill_create_ip = !empty($remote_addr_info['remote_addr'])? $remote_addr_info['remote_addr']:'127.0.0.1'; // 客户端ip $request->notify_url = config('app_host').'/api/index/pay/pay/wx_notify_url'; // 异步通知地址 // 调用接口 $result = $pay->execute($request); $result['data']= $result['code_url']; $result['code'] = 0; $result['msg'] = '成功'; return json_encode($result); } } //微信支付成功回调 public function wx_notify_url(RequestInterface $request,ResponseInterface $response) { $xml = $request->getBody()->getContents(); $json = json_encode(simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA)); $data = json_decode($json,true); //目前微信支付代码为4,有可能变动; $payment_all = Db::table('payments')->where('id',4)->first(); $payment = json_decode($payment_all['config'],true); $params = new \Yurun\PaySDK\Weixin\Params\PublicParams(); $params->appID = isset($payment['appID'])?$payment['appID']:''; $params->mch_id =isset($payment['mch_id'])?$payment['mch_id']:''; $params->sign_type = 'MD5'; $params->key =isset($payment['key'])?$payment['key']:''; // SDK实例化,传入公共配置 $pay = new \Yurun\PaySDK\Weixin\SDK($params); $payNotify = new class extends \Yurun\PaySDK\Weixin\Notify\Pay { protected function __exec() { $this->reply(true, 'OK'); } }; if(!empty($data)) { $data_sign = $data['sign']; //sign不参与签名算法 unset($data['sign']); $sign = $pay->sign($data); if ($sign ===$data_sign && ($data['return_code'] === 'SUCCESS') && ($data['result_code'] === 'SUCCESS')) { //订单处理,修改订单状态 $order_sn = $data['out_trade_no']; Db::table('order')->where('order_sn', $order_sn)->update(['status' => 1,'payments_id'=>4,'paytime'=>time()]); $order = Db::table('order')->where('order_sn', $order_sn)->first(); if(!empty($order)) { if($order['order_type']==1) { //如果该订单是充值预付款 $this->change_org($order_sn); } elseif($order['order_type']==5) { //购买产品 $this->change_product($order); } elseif($order['order_type']==6) { //产品升级 $this->level_product($order); } } } } $payNotify->swooleRequest = $request; $payNotify->swooleResponse = $response; $pay->notify($payNotify); return $payNotify->swooleResponse; } (2)支付宝支付与回调 //支付宝付款 public function alipay(RequestInterface $request,ResponseInterface $response) { $order_sn = $request->input('order_sn',''); $data = Db::table('order')->where('order_sn',$order_sn)->first(); $payment_all = Db::table('payments')->where('id',3)->first(); $payment = json_decode($payment_all['config'],true); $notify_url = config('app_host').'/api/index/pay/pay/alipay_notify'; $return_url = config('app_host').'/agent/dashboard/paymoney';//前台页面的url路径 if(!empty($payment) && !empty($data)) { if(empty($payment['appID'])) { $result['code'] = -1; $result['msg'] = '支付宝APPID未填写,请先完善资料!'; return json_encode($result); } if(empty($payment['private_key'])) { $result['code'] = -1; $result['msg'] = '支付宝私钥未填写,请先完善资料!'; return json_encode($result); } if(empty($payment['public_key'])) { $result['code'] = -1; $result['msg'] = '支付宝公钥未填写,请先完善资料!'; return json_encode($result); } $params = new \Yurun\PaySDK\AlipayApp\Params\PublicParams; $params->appID = $payment['appID']; $params->appPrivateKey = $payment['private_key']; $params->appPublicKey = $payment['public_key']; $pay = new \Yurun\PaySDK\AlipayApp\SDK($params); $pay->swooleRequest = $request; $pay->swooleResponse = $response; // 支付接口 $request = new \Yurun\PaySDK\AlipayApp\Page\Params\Pay\Request; $request->notify_url = $notify_url; // 支付后通知地址(作为支付成功回调,这个可靠) $request->return_url = $return_url; // 支付后跳转返回地址 $request->businessParams->out_trade_no = $data['order_sn']; // 商户订单号 $request->businessParams->total_amount = doubleval($data['order_price']); // 价格 $request->businessParams->subject = $data['order_name']; // 商品标题 $url = ''; $pay->prepareExecute($request, $url); $result['data'] = $url; $result['code'] = 0; $result['msg'] = '成功'; return json_encode($result); } } //支付宝回调 public function alipay_notify(RequestInterface $request) { $data = $request->all();; if(!empty($data) && $data['trade_status'] == 'TRADE_SUCCESS') { //订单处理,修改订单状态 $order_sn = $data['out_trade_no']; Db::table('order')->where('order_sn', $order_sn)->update(['status' => 1,'payments_id'=>4,'paytime'=>time()]); $order = Db::table('order')->where('order_sn', $order_sn)->first(); if(!empty($order)) { if($order['order_type']==1) { //如果该订单是充值预付款 $this->change_org($order_sn); } } return 'success'; }else{ return 'failure'; } }

(3)针对微信扫码支付的定时查询数据库,支付成功后跳转

if(this.form1.pay_id == 4){ this.$http.post('/index/wxpay',{order_sn: this.form2.order_sn}).then(res => { if (res.data.code === 0) { this.text = res.data.data; this.loading1 = false; this.active = 1; this.loading2 = true; const paystatus_timer = setInterval(() =>{ this.$http.post('/index/pay_status',{order_sn: this.form2.order_sn}).then(res => { if (res.data.code === 0) { if(res.data.data.status == 1){ this.loading2 = false; this.active = 2; clearInterval(paystatus_timer); } } else if (res.data.msg) { this.$message.error(res.data.msg); } }).catch(e => { this.$message.error(e.message); console.error(e); }); }, 8000); // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。 this.$once('hook:beforeDestroy', () => { clearInterval(paystatus_timer); }); } else if (res.data.msg) { this.$message.error(res.data.msg); } }).catch(e => { this.$message.error(e.message); console.error(e); }); } if(this.form1.pay_id == 3){ this.$http.post('/index/alipay',{order_sn: this.form2.order_sn}).then(res => { if (res.data.code === 0) { window.location.href = res.data.data; } else if (res.data.msg) { this.$message.error(res.data.msg); } }).catch(e => { this.$message.error(e.message); console.error(e); }); }

另外附件:thinkphp的yurunsoft/paysdk使用教程

有问题欢迎加微信 xingkong0126沟通哈



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3