123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace app\api\controller;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use app\api\controller\Evaluate;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use function AlibabaCloud\Client\value;
- class Pay extends Controller
- {
-
- public static function wxPay($name='订单支付',$out_trade_no,$total_fee,$notify_url,$trade_type = 'JSAPI',$openid,$type = 1){
- try{
- if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)) return false;
- if($type == 1){
- $app = Factory::payment(config('app.wx_pay'));
- }else{
- $app = Factory::payment(config('app.worker_wx_pay'));
- }
- $parameter = array(
- 'body' => $name,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee*100,
- 'notify_url' => $notify_url,
- 'trade_type' => $trade_type,
- );
- if($trade_type != 'APP'){
- $parameter['openid'] = $openid;
- }
- $result = $app->order->unify($parameter);
- $jssdk = $app->jssdk;
- $config = $jssdk->sdkConfig($result['prepay_id']);
- return $config;
- }catch (Exception $e){
- return false;
- }
- }
-
- public function goodsOrderNotify()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'goods_order');
-
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('goods_order')->where('pay_no',$pay_no)->find();
-
- if ($message['result_code'] == 'SUCCESS') {
- Db::startTrans();
- $goods_info = Db::table('store_goods')->find($order_info['goods_id']);
- $collect_num = Db::table('goods_collect')
- ->where(['goods_id'=>$goods_info['id']])
- ->where('status','in',[1,3])->count();
- $serial = intval($collect_num) + 1;
- $user_info =Db::table('store_member')->find( $order_info['uid']);
- $collect_info = [
- 'user_id'=> $order_info['uid'],
- 'goods_id'=> $order_info['goods_id'],
- 'goods_cover'=> $order_info['goods_cover'],
- 'goods_name'=> $order_info['goods_name'],
- 'serial' => $serial,
- 'from_id' => $order_info['id'],
- 'sc_name' =>$user_info['name'],
- 'issuer' =>$goods_info['issuer'],
- 'framer' =>$goods_info['goods_auth'],
- 'coll_intro'=>$goods_info['coll_intro'],
- ];
- $collect_info = array_merge($collect_info,get_goods_hash($order_info['uid'],$goods_info['id'],$serial));
- Db::table('goods_collect')->insert($collect_info);
- goods_sell_info($goods_info,$order_info['uid'],$order_info['id']);
- $res = Db::table('goods_order')->where('id',$order_info['id'])->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>1,'status'=>1]);
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true;
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
-
- public function crystalRecharge()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'crystal_order');
-
- $pay_no = $message['out_trade_no'];
- $order_info = Db::name('crystal_order')->where('pay_no',$pay_no)->find();
-
- if ($message['result_code'] == 'SUCCESS') {
- Db::startTrans();
- $user_info = Db::table('store_member')->find($order_info['uid']);
-
- Db::table('store_member')->where('id',$order_info['uid'])->update(['crystal'=>bcadd($user_info['crystal'],$order_info['crystal'],2)]);
-
- crystal_log($order_info['uid'],$order_info['crystal'],'元石充值',1,$order_info['id']);
-
- $res = Db::table('crystal_order')->where('id',$order_info['id'])
- ->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>1,'status'=>1]);
- if(!$res){
- Db::rollback();
- }else{
- Db::commit();
- }
- return true;
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
-
- public function payResultLog($message,$table_name)
- {
-
- $ret_arr = [];
- $ret_arr['transaction_id'] = isset($message['transaction_id']) ?$message['transaction_id']: '';
- $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
- $ret_arr['return_code'] = isset($message['return_code']) ?$message['return_code']: '';
- $ret_arr['result_code'] = isset($message['result_code']) ?$message['result_code']: '';
- $ret_arr['create_at'] = date('Y-m-d H:i:s');
- $ret_arr['order_table'] = $table_name;
- $ret_arr['result'] = json_encode($message);
- Db::table('order_pay_result')->insert($ret_arr);
- }
- }
|