orderCloseCallback($data); return; } $openapiurl="https://t.masget.com/openapi/rest"; $session = "iba3t9dqln8etmcaceq1xtqr7urevia1"; $appid="910000198"; $secretkey="gRAZZKiNw0UbTuF9"; $api=new ApiHelperoneService(); $api->init($openapiurl,$appid,$session,$secretkey); $security = new security(); $decrypted = $security->decrypt($GLOBALS['token_secretkey'], $GLOBALS['token_secretkey'], $json['Data']); $pattern = "/\\x00/"; $decrypted = preg_replace($pattern, "", $decrypted); $result = json_decode($decrypted, true); $batchcode = $result['ordernumber']; $this->orderlog->info("{$batchcode}订单支付回调", $result); $order = Order::where('batchcode', $batchcode)->find(); if(!$order){ return; } if($order->status != 1 || $order->pay_status == 2){ return; } $goodss = OrderGoodsDetail::where('batchcode', $batchcode)->where('is_deleted', 0)->field('goods_islicode')->select()->toArray(); $goodss = array_column($goodss, 'goods_islicode'); Goods::whereIn('goods_islicode', $goodss)->inc('sale_count')->update(); // $proportion = ""; // $transaction_islicode = $this->applyContractCode($order->goods_islicode,$order->buy_islicode, $order->entrust_islicode, $order->transaction_count, $order->use_years, $proportion); // $order->transaction_islicode = $transaction_islicode; $order->status = 2; $order->pay_status = 2; $order->paymenttime = date('Y-m-d H:i:s'); $order->payid = $result['payorderid']; $order->save(); $this->payFinishOperate($batchcode); $this->invoiceBuyOperate($batchcode); $this->invoiceSellerOperate($batchcode); } public function test(){ // $this->payFinishOperate("1648890413394325"); // $this->payFinishOperate("1648539998374081"); // $this->payFinishOperate("1648631147826352"); // return $this->payFinishOperate("1650960843614890"); $this->invoiceBuyOperate("1651135887697535"); $this->invoiceSellerOperate("1651135887697535"); // $this->invoiceOperate('123'); } public function payComplete(){ $order = Order::where('status', 2)->whereTime('paymenttime', 'between', [date('Y-m-d H:i:s', strtotime("-7 day")), date("Y-m-d H:i:s")])->field('batchcode')->select()->toArray(); foreach($order as $data){ $this->payFinishOperate($data['batchcode']); } } public function payFinishOperate($batchcode){ $order = Order::where('batchcode', $batchcode)->find(); if(!$order){ // throw new \think\Exception('没有该订单', 400); return; } if($order->pay_status != 2){ // throw new \think\Exception('订单还未支付成功', 400); // return; } $status = $order->pay_status; $close_status = Order::where('batchcode', $batchcode)->value('close_status'); $type = Order::where('batchcode', $batchcode)->value('type'); $buy_username = OrderUser::where('islicode', $order->buy_islicode)->where('batchcode', $batchcode)->value('name'); // 申请合约关联编码 $buy_islicode = $order->buy_islicode; // $buy_islicode = "999998-000000000023-0"; $paytime = $order->paymenttime; $synchInfo = []; $contractcode_arr = []; $order_detail = OrderGoodsDetail::where('batchcode', $batchcode)->where('is_deleted', 0)->select(); $order_detail->each(function ($item) use($buy_islicode, $paytime, $close_status, $type, $buy_username, &$synchInfo, &$contractcode_arr, $status){ $goods_islicode = $item['goods_islicode']; if($item['goods_entrust'] == 1 || $item['charges_type'] == 1){ $date = "永久"; $transaction_count = 1; }else{ $date = date('Y-m-d', strtotime("+".$item['transaction_count']." year", strtotime($paytime))); $transaction_count = $item['transaction_count']; } if($item['money'] > 0 && $status != 2){ return $item; } if(empty($item['contract_code'])){ $contractcode = $this->applyContractCode($item['sourcedata_islicode'], $buy_islicode, $transaction_count, $date, $item['batchcode']); }else{ $contractcode = $item['contract_code']; } $contractcode_arr[$item['id']] = $contractcode; if(!empty($contractcode)){ OrderGoodsDetail::where('id', $item['id'])->where('is_deleted', 0)->update(['contract_code' => $contractcode]); $synchInfo[] = [ "userId" => $buy_islicode, "requestIsliFlag" => env('isli.channel_isli'), "goodsIslicode" => $item['goods_islicode'], "goodsName" => $item['goods_name'], "orderIslicode" => $contractcode, "batchcode" => $item['batchcode'], "chargesType" => $item['money'], "entrustMonth" => ($item['contractual_period'] == 1 ? "永久" : ($item['contractual_period'] == 2 ? "单次" : "周期")), "goodsEntrust" => ($item['goods_entrust'] == 1 ? "转让" : "许可"), "goodsOwnership" => $item['goods_ownership_str'], "goodsType" => ($item['goods_type'] == 1 ? "文化资源数据" : "文化数字内容"), "payStatus" => 1, "closeStatus" => ( $close_status - 1 ), "transactionCount" => $item['transaction_count'], "type" => ( $type == 1 ? "委托订单" : ($type == 2 ? "认证订单" : "服务订单") ), "buyUsername" => $buy_username, "entrustIslicode" => $item['entrust_islicode'], "entrustUsername" => OrderUser::where('islicode', $item['entrust_islicode'])->where('batchcode', $item['batchcode'])->value('name') ]; } }); if(count($contractcode_arr) <= 0){ return; } $order_detail = $order_detail->toArray(); // $path = env("app.tianlang_entrust_url") . "/api/v1/synchInfo"; // http://45.251.21.170:51317/dist //todo 请求交易结算平台同步订单数据到分发平台获取下载地址 接口3.1 $path = env("app.real_url") . "/dist/api/v1/synchInfo"; $sign = parent::createSign("distribute"); $headers = array( CURLOPT_HTTPHEADER => array( "dist_token:{$sign}", "Content-Type: application/json" ) ); $result = Http::post($path, json_encode($synchInfo), $headers); $this->orderlog->info("{$batchcode}订单获取下载地址", $result); if($result['code'] != 200){ // throw new \think\Exception($result['msg'], 400); foreach($order_detail as $detail) { $source_url_arr = json_decode($detail['source_download'], true); if(empty($source_url_arr)){ $source_url_arr = []; } for($i=0;$i < count($source_url_arr);$i++) { if($source_url_arr[$i]['status'] != 4){ $source_url_arr[$i]['status'] = 2; } } $source_url_arr = json_encode($source_url_arr); OrderGoodsDetail::where('id', $detail['id'])->where('is_deleted', 0)->update(['source_download' => $source_url_arr]); } return; } $result = json_decode($result['data'], true); if($result['resultCode'] != "00000000"){ // throw new \think\Exception($result['resultMsg'], 400); foreach($order_detail as $detail) { $source_url_arr = json_decode($detail['source_download'], true); if(empty($source_url_arr)){ $source_url_arr = []; } for($i=0;$i < count($source_url_arr);$i++) { if($source_url_arr[$i]['status'] != 4){ $source_url_arr[$i]['status'] = 2; } } $source_url_arr = json_encode($source_url_arr); OrderGoodsDetail::where('id', $detail['id'])->where('is_deleted', 0)->update(['source_download' => $source_url_arr]); } return; } $source_url = $result['data']['source_url']; $source_url = str_replace('{isliCode}/{contractISLICode}', '', $source_url); // var_dump($source_url);die; $order_goods_source = OrderGoodsSource::where('batchcode', $batchcode)->select()->toArray(); // $sign = parent::createSign("distribute"); foreach($order_detail as $detail){ $source_url_arr = json_decode($detail['source_download'], true); // $identifiers = explode(',', $detail['identifiers']); // foreach($order_goods_source as $value){ if(empty($source_url_arr)){ $source_url_arr = []; } for($i=0;$i < count($source_url_arr);$i++){ if($source_url_arr[$i]['status'] > 3){ continue; } if(empty($contractcode_arr[$detail['id']])){ continue; } //todo 请求依据同步订单信息返回的地址获取解密资源地址 接口3.2 $res = Http::get($source_url . "{$source_url_arr[$i]['islicode']}/{$contractcode_arr[$detail['id']]}", [], $headers); $this->orderlog->info("{$batchcode}订单获取资源下载地址", $res); if($res['code'] != 200){ // throw new \think\Exception($res['msg'], 400); if($source_url_arr[$i]['status'] != 4){ $source_url_arr[$i]['status'] = 3; } continue; } $result = json_decode($res['data'], true); if($result['resultCode'] != "00000000"){ // throw new \think\Exception($result['resultMsg'], 400); if($source_url_arr[$i]['status'] != 4){ $source_url_arr[$i]['status'] = 3; } continue; } if(!empty($result['data']['url'])){ $source_url_arr[$i]['url'] = $result['data']['url']; $source_url_arr[$i]['status'] = 4; }else{ // $source_url_arr[$i]['url'] = $result['data']['url']; $source_url_arr[$i]['status'] = 5; } } // } $source_url_arr = json_encode($source_url_arr); OrderGoodsDetail::where('id', $detail['id'])->where('is_deleted', 0)->update(['source_download' => $source_url_arr]); } // $update_source = []; // if(!empty($value['target_data'])){ // $value['target_data'] = json_decode($value['target_data'], true); // $value['target_data']['source_url'] = $source_url_arr; // $update_source['target_data'] = json_encode($value['target_data']); // }else{ // $value['source_data'] = json_decode($value['source_data'], true); // $value['source_data']['source_url'] = $source_url_arr; // $update_source['source_data'] = json_encode($value['source_data']); // } // OrderGoodsSource::where('id', $value['id'])->update($update_source); } public function applyContractCode($goods_islicode, $buy_islicode, $count, $date, $batchcode){ $ratio_setting = AccountRatioSetting::where('is_deleted', 0)->where('status', 1)->find(); $entrust_ratio_detail = AccountRatioDetail::where('setting_id', $ratio_setting->id)->where('role_type', 1)->find(); $ratio = 0; $entrust_number = 0; if($entrust_ratio_detail['calculate'] == 1){ $entrust_number = $entrust_ratio_detail['ratio']; }else{ $entrust_number = $entrust_ratio_detail['amount']; } $ratio = round(1 - ($entrust_number / 100), 4); //todo 请求申请合约关联编码 接口1.5 $path = env("app.tianlang_entrust_url") . "/consignation/v1/applylinkcode"; $post = [ "contractISLIFlag" => $goods_islicode, "buyerISLIFlag" => $buy_islicode, "shareProportion" => $ratio, "purchaseQuantity" => $count, "authorizationDate" => $date, ]; $sign = parent::createSign("apply_code"); $headers = array( CURLOPT_HTTPHEADER => array( "cloudhub_token:{$sign}", "channel_isli_flag:".env('isli.channel_isli'), "Content-Type: application/json" ) ); $result = Http::post($path, json_encode($post), $headers); $this->orderlog->info("{$batchcode}订单获取合约关联编码请求参数", $post); $this->orderlog->info("{$batchcode}订单获取合约关联编码", $result); if($result['code'] != 200){ // throw new \think\Exception($result['msg'], 400); return ""; } $result = json_decode($result['data'], true); if($result['resultCode'] != "00000000"){ return ""; // throw new \think\Exception($result['resultMsg'], 400); } return $result['data'][$buy_islicode]; } public function invoiceBuyOperate($batchcode){ $pdf_path = $this->initDir(); //买家发票 $order = Order::where('batchcode', $batchcode)->find(); $buyuser_isli = $order->buy_islicode; $userinfo = OrderUser::where('batchcode', $batchcode)->where('islicode', $buyuser_isli)->find(); $invoice_data = [ "buyerName" => $userinfo['name'], //购买方名称 "buyerType" => '01',//购买方类型01:企业 "buyerTaxNo" => $userinfo['uscc'],//购买方税号 // "totalAmountTax" => $order->total_service_charge,//总金额含税 ]; $order_detail = OrderGoodsDetail::where('batchcode', $batchcode)->where('is_deleted', 0)->select()->toArray(); $invoice = new InvoiceManagementService(); $data = $this->orderMoneySplit($order_detail, $batchcode); // foreach($order_detail as $val){ // $invoice_data['manualOrderDetails'][] = [ // 'amount' => $val['service_charge'], //金额 // 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 // 'itemName' => $val['goods_name'], //商品名称 // 'itemTaxCode' => '3049900000000000000', //商品税号 // 'taxIncluded' => '1', //1:含税0:不含税 // 'taxRate' => '0.06',//税率 // 'yhzcbs' => '0',//优惠政策标识 // ]; // } // if($order->total_service_charge <= 0){ // return; // } for($i=0;$iorderlog->info("{$batchcode}买家发票请求", $data[$i]); $result = $invoice->RespondToInvoices($data[$i]); $this->orderlog->info("{$batchcode}买家发票结果", $result); if ($result['operateCode'] == 'S') { // $amount = 0; // foreach ($result['datas']['invoiceDetail'] as $value) { // $amount += $value['amount']; // } $status = get_headers($result['datas']['pdfUrl']); $ticketurl = ""; if (strpos($status[0], "200")) { $pdf_content = file_get_contents($result['datas']['pdfUrl']); file_put_contents('./' . $pdf_path . "{$result['datas']['serialNo']}.pdf", $pdf_content); $ticketurl = $pdf_path . "{$result['datas']['serialNo']}.pdf"; } $insert = [ "batchcode" => $batchcode, "islicode" => $buyuser_isli, "ticketid" => $result['datas']['serialNo'], "ticketmoney" => $data[$i]['totalAmountTax'], "ticketurl" => $ticketurl, "pdfUrl" => $result['datas']['pdfUrl'], "ticket_status" => 3, "tickettime" => date('Y-m-d H:i:s'), "ticketApiMessage" => "-", ]; // OrderUser::where('batchcode', $batchcode)->where('islicode', $buyuser_isli)->update($update); (new Ticket())->insert($insert); } else { $insert = [ "batchcode" => $batchcode, "islicode" => $buyuser_isli, "ticketid" => "", "ticketmoney" => $data[$i]['totalAmountTax'], // "ticketurl" => $pdf_path . "{$result['datas']['serialNo']}.pdf", "pdfUrl" => $result['datas']['pdfUrl'], "ticket_status" => 4, "tickettime" => date('Y-m-d H:i:s'), "ticketApiMessage" => $result['message'], ]; // OrderUser::where('batchcode', $batchcode)->where('islicode', $buyuser_isli)->update($update); (new Ticket())->insert($insert); } } } public function orderMoneySplit($data, $batchcode){ $account_ratio_id = Order::where('batchcode', $batchcode)->value('account_ratio_id'); $ratio_detail = AccountRatioDetail::where('setting_id', $account_ratio_id)->where('role_type', 2)->find(); // 金额按9999拆分 $result = []; $index = 0; foreach($data as $key => $val){ $money = $val['service_charge']; if($money <= 0){ continue; } if($money > 9999){ while($money){ $amount = $money - 9999; if($amount > 0){ $price = 9999; }elseif($amount < 0){ $price = $money; } $index++; $result[$index]['totalAmountTax'] = $price; $result[$index]['manualOrderDetails'][] = [ 'amount' => $price, //金额 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 'itemName' => "*现代服务*交易佣金", //商品名称 'itemTaxCode' => '3049900000000000000', //商品税号 'taxIncluded' => '1', //1:含税0:不含税 'taxRate' => '0.06',//税率 'yhzcbs' => '0',//优惠政策标识 ]; if($amount < 0){ break; } $money = $amount; } }elseif ($money == 9999){ $index++; $result[$index]['totalAmountTax'] = $money; $result[$index]['manualOrderDetails'][] = [ 'amount' => $money, //金额 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 'itemName' => "*现代服务*交易佣金", //商品名称 'itemTaxCode' => '3049900000000000000', //商品税号 'taxIncluded' => '1', //1:含税0:不含税 'taxRate' => '0.06',//税率 'yhzcbs' => '0',//优惠政策标识 ]; }elseif($money < 9999){ if(isset($result[$index])){ $this_money = $result[$index]['totalAmountTax']; }else{ $this_money = 0; } if($this_money + $money > 9999){ $this_money = 0; $index++; } $result[$index]['totalAmountTax'] = $this_money + $money; $result[$index]['manualOrderDetails'][0] = [ 'amount' => $result[$index]['totalAmountTax'], //金额 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 'itemName' => "*现代服务*交易佣金", //商品名称 'itemTaxCode' => '3049900000000000000', //商品税号 'taxIncluded' => '1', //1:含税0:不含税 'taxRate' => '0.06',//税率 'yhzcbs' => '0',//优惠政策标识 ]; } } return array_values($result); } public function orderSellerMoneySplit($data, $batchcode){ $account_ratio_id = Order::where('batchcode', $batchcode)->value('account_ratio_id'); $ratio_detail = AccountRatioDetail::where('setting_id', $account_ratio_id)->where('role_type', 3)->find(); // 金额按9999拆分 $result = []; $index = 0; foreach($data as $key => $val){ $money = round($val['goods_price'] * ($ratio_detail->ratio / 100), 2); if($money <= 0){ continue; } if($money > 9999){ while($money){ $amount = $money - 9999; if($amount > 0){ $price = 9999; }elseif($amount < 0){ $price = $money; } $index++; $result[$index]['totalAmountTax'] = $price; $result[$index]['manualOrderDetails'][] = [ 'amount' => $price, //金额 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 'itemName' => "*现代服务*交易佣金", //商品名称 'itemTaxCode' => '3049900000000000000', //商品税号 'taxIncluded' => '1', //1:含税0:不含税 'taxRate' => '0.06',//税率 'yhzcbs' => '0',//优惠政策标识 ]; if($amount < 0){ break; } $money = $amount; } }elseif ($money == 9999){ $index++; $result[$index]['totalAmountTax'] = $money; $result[$index]['manualOrderDetails'][] = [ 'amount' => $money, //金额 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 'itemName' => "*现代服务*交易佣金", //商品名称 'itemTaxCode' => '3049900000000000000', //商品税号 'taxIncluded' => '1', //1:含税0:不含税 'taxRate' => '0.06',//税率 'yhzcbs' => '0',//优惠政策标识 ]; }elseif($money < 9999){ if(isset($result[$index])){ $this_money = $result[$index]['totalAmountTax']; }else{ $this_money = 0; } if($this_money + $money > 9999){ $this_money = 0; $index++; } $result[$index]['totalAmountTax'] = $this_money + $money; $result[$index]['manualOrderDetails'][0] = [ 'amount' => $result[$index]['totalAmountTax'], //金额 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 'itemName' => "*现代服务*交易佣金", //商品名称 'itemTaxCode' => '3049900000000000000', //商品税号 'taxIncluded' => '1', //1:含税0:不含税 'taxRate' => '0.06',//税率 'yhzcbs' => '0',//优惠政策标识 ]; } } return array_values($result); } public function invoiceSellerOperate($batchcode){ $pdf_path = $this->initDir(); // 卖家发票 $invoice = new InvoiceManagementService(); $buyuser_isli = Order::where('batchcode', $batchcode)->value("buy_islicode"); $userinfo = OrderUser::where('batchcode', $batchcode)->where('islicode', '<>', $buyuser_isli)->select()->toArray(); foreach($userinfo as $val){ // $invoice_data = [ // "buyerName" => $val['name'], //购买方名称 // "buyerType" => '01',//购买方类型01:企业 // "buyerTaxNo" => $val['uscc'],//购买方税号 //// "totalAmountTax" => $order->total_service_charge,//总金额含税 // ]; // $total_service_charge = 0; $order_detail = OrderGoodsDetail::where('batchcode', $batchcode)->where('is_deleted', 0)->where('entrust_islicode', $val['islicode'])->select()->toArray(); $data = $this->orderSellerMoneySplit($order_detail, $batchcode); // foreach($order_detail as $value){ // $total_service_charge += $value['service_charge']; // $invoice_data['manualOrderDetails'][] = [ // 'amount' => $value['service_charge'], //金额 // 'invoiceNature' => '0', //0:正常 1:折扣 2:被折扣 // 'itemName' => $value['goods_name'], //商品名称 // 'itemTaxCode' => '3049900000000000000', //商品税号 // 'taxIncluded' => '1', //1:含税0:不含税 // 'taxRate' => '0.06',//税率 // 'yhzcbs' => '0',//优惠政策标识 // ]; // } // if($total_service_charge <= 0){ // return; // } // $invoice_data['totalAmountTax'] = $total_service_charge; for($i=0;$iorderlog->info("{$batchcode}卖家发票请求", $data[$i]); $result = $invoice->RespondToInvoices($data[$i]); $this->orderlog->info("{$batchcode}卖家发票结果", $result); if ($result['operateCode'] == 'S') { // $amount = 0; // foreach ($result['datas']['invoiceDetail'] as $value) { // $amount += $value['amount']; // } $status = get_headers($result['datas']['pdfUrl']); $ticketurl = ""; if (strpos($status[0], "200")) { $pdf_content = file_get_contents($result['datas']['pdfUrl']); file_put_contents('./' . $pdf_path . "{$result['datas']['serialNo']}.pdf", $pdf_content); $ticketurl = $pdf_path . "{$result['datas']['serialNo']}.pdf"; } $insert = [ "batchcode" => $batchcode, "islicode" => $val['islicode'], "ticketid" => $result['datas']['serialNo'], "ticketmoney" => $data[$i]['totalAmountTax'], "ticketurl" => $ticketurl, "pdfUrl" => $result['datas']['pdfUrl'], "ticket_status" => 3, "tickettime" => date('Y-m-d H:i:s'), "ticketApiMessage" => "-" ]; // OrderUser::where('batchcode', $batchcode)->where('islicode', $buyuser_isli)->update($update); (new Ticket())->insert($insert); } else { $insert = [ "batchcode" => $batchcode, "islicode" => $val['islicode'], "ticketid" => "", "ticketmoney" => $data[$i]['totalAmountTax'], // "ticketurl" => $pdf_path . "{$result['datas']['serialNo']}.pdf", "pdfUrl" => $result['datas']['pdfUrl'], "ticket_status" => 4, "tickettime" => date('Y-m-d H:i:s'), "ticketApiMessage" => $result['message'], ]; // OrderUser::where('batchcode', $batchcode)->where('islicode', $buyuser_isli)->update($update); (new Ticket())->insert($insert); } } } } public function initDir(){ if(!file_exists("./uploads")){ mkdir("./uploads"); } if(!file_exists("./uploads/pdf")){ mkdir("./uploads/pdf"); } $date = date('Ymd'); if(!file_exists("./uploads/pdf/{$date}")){ mkdir("./uploads/pdf/{$date}"); } return "/uploads/pdf/{$date}/"; } }