dealData($result); if ($return){ $arr = array( 'return_code' => 'SUCCESS', 'return_msg' => 'OK', ); return $this->arrayToXml($arr); }else{ file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND); } } } /** * 支付宝支付--支付成功回调订单 */ public function alipayOrderNotify(){ $result = input('post.'); if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') { $return = $this->dealData($result); if ($return){ echo 'success'; }else{ file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND); } } } /** * 处理数据库信息 * @param $result * @return bool */ function dealData($result){ Db::startTrans(); try { $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find(); // if($order['status'] != 0){ // return true; // } Db::name('store_order') ->where('order_no',$result['out_trade_no']) ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]); $info = Db::name('store_order_info')->where('order_id',$order['id'])->count(); if ($info>=$order['num']){ Db::commit(); return true; } $array = []; for ($i=0;$i<$order['num'];$i++){ //获取排名 $rank = getRanking($order['c_id'])+1; $tag = getTag($order['c_id'],$rank,$order['inventory']); saveRanking($order['c_id']); $company = '象寻数字科技(上海)有限公司'; // $hash = getCompanyHash($order['c_id']); // $company_hash = $hash['hash']; // $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid'); // $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s'); //Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]); $company_hash = Db::name('hash2')->where('goods_id',$order['c_id'])->where('success',1)->field('hash,ddcid')->find(); $company_hash_time = date('Y-m-d H:i:s'); $collectors_hash = ''; $date = [ 'order_id'=>$order['id'], 'order_no'=>get_order_sn(), 'tag'=>$tag, 'mid'=>$order['mid'], 'c_id'=>$order['c_id'], 'pro_info'=>$order['pro_info'], 'company'=>$company, 'company_hash'=>$company_hash['hash'], 'company_hash_time'=>$company_hash_time, 'ddcid'=>$company_hash['ddcid'], 'collectors_hash'=>$collectors_hash, 'collectors_hash_time'=>'', 'type'=>2 ]; $array[] = $date; } Db::name('store_order_info')->insertAll($array); //送积分 $by_collection_integral = getConfigValue('by_collection_integral'); if ($by_collection_integral){ $by_collection_integral = bcmul($by_collection_integral,$order['num']); memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']); } Db::commit(); return true; } catch (\Exception $e){ Db::rollback(); return false; } } /** * 数组转xml * @ApiInternal */ public function arrayToXml($arr) { $xml = ""; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . ""; } else $xml .= "<" . $key . ">"; } $xml .= ""; return $xml; } /** * 微信充值支付--支付成功回调订单 */ public function WxRechargeNotify(){ $payXml = file_get_contents("php://input"); //将xml格式转化为json格式 $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA)); //将json格式转成数组格式 $result['out_trade_no'] $result = json_decode($jsonXml, true); file_put_contents("wx_recharge_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { $total_fee = $result['total_fee']; $total_fee1 = $total_fee / 100; $transaction_id = $result['transaction_id']; $order = Db::name('SystemRecharge')->where('number',$result['out_trade_no'])->find(); if($order['status'] == 0) { if ($order['need_pay'] == $total_fee1) { Db::startTrans(); try { $order_upd['status'] = 1; $order_upd['pay_time'] = time(); $order_upd['pay_way'] = '微信'; $order_upd['pay_money'] = $total_fee1; $order_upd['pay_number'] = $transaction_id; Db::name('SystemRecharge') ->where('number',$result['out_trade_no']) ->update($order_upd); $this -> addMoney($order['mem_id'],$order['money']); Db::commit(); $arr = array( 'return_code' => 'SUCCESS', 'return_msg' => 'OK', ); return $this->arrayToXml($arr); } catch (\Exception $e) { Db::rollback(); $msg = $e->getMessage(); file_put_contents("wx_recharge_pay_error.txt", $msg, FILE_APPEND); } }else{ file_put_contents("wx_recharge_pay_error.txt", '金额验证失败', FILE_APPEND); } }else{ file_put_contents("wx_recharge_pay_error.txt", '订单状态错误', FILE_APPEND); } } } /** * 支付宝充值支付--支付成功回调订单 */ public function alipayRechargeNotify(){ $result = input('post.'); $_POST = $result; file_put_contents("ali_recharge_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND); if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') { if ($_POST['trade_status'] == 'TRADE_SUCCESS') { $out_trade_no = $_POST['out_trade_no'];//订单号 $buyer_pay_amount = $_POST['receipt_amount'];//实际支付金额 $order = Db::name('SystemRecharge')->where('number',$out_trade_no)->find(); if($order['status'] == 0) { if ($order['need_pay'] == $buyer_pay_amount) { Db::startTrans(); try { $order_upd['status'] = 1; $order_upd['pay_time'] = time(); $order_upd['pay_way'] = '支付宝'; $order_upd['pay_money'] = $buyer_pay_amount; $order_upd['pay_number'] = $_POST['trade_no']; Db::name('SystemRecharge') ->where('number',$result['out_trade_no']) ->update($order_upd); $this -> addMoney($order['mem_id'],$order['money']); Db::commit(); echo 'success'; } catch (\Exception $e) { Db::rollback(); $msg = $e->getMessage(); file_put_contents("ali_recharge_pay_error.txt", $msg, FILE_APPEND); } }else{ file_put_contents("ali_recharge_pay_error.txt", '金额验证失败', FILE_APPEND); } }else{ file_put_contents("ali_recharge_pay_error.txt", '订单状态错误', FILE_APPEND); } } else { file_put_contents("ali_recharge_pay_error.txt", '验证失败', FILE_APPEND); } } } function addMoney($mem_id,$money){ if($money <= 0) return ['code'=>true,'message'=>'成功']; $old_money = Db::name('StoreMember') -> where('id',$mem_id)->field('id,money')->find(); Db::name('StoreMember') -> where('id',$mem_id)->setInc('money',$money); $sre_data['mem_id']=$mem_id; $sre_data['change']=$money; $sre_data['pm']=1; $sre_data['old_money']=$old_money['money']; $sre_data['reason']='在线充值'; $sre_data['time']=time(); Db::name('SystemMoneyRecord')->insert($sre_data); } public function validateApplePay() { $receipt_data = input('receipt_data'); //receipt_data参数 if (!$receipt_data) $this->error('receipt_data参数错误'); $order_no = input('order_no'); //订单号 if (!$order_no) $this->error('订单号参数错误'); $order_type = input('order_type'); //订单类型 1订单 2充值 if (!$order_type) $this->error('订单类型参数错误'); $pay_money = input('pay_money'); //支付金额 if (!$pay_money) $this->error('支付金额参数错误'); // 验证参数 if (strlen($receipt_data) < 20) { $this->error("非法参数"); } // // 请求验证 $html = $this->acurl($receipt_data); $data = json_decode($html, true); // 如果是沙盒数据 则验证沙盒模式 if ($data['status'] == '21007') { // 请求验证 $html = $this->acurl($receipt_data, 1); $data = json_decode($html, true); $data['sandbox'] = '1'; } file_put_contents("validate_apple_pay_error.txt", $html . "\n" . json_encode($data) . "\n" . "\n", FILE_APPEND); // 判断是否购买成功 if (intval($data['status']) === 0) { switch ($order_type){ case '1': //订单 $result['out_trade_no'] = $order_no; $return = $this->dealData($result); if ($return){ $this->success("操作成功"); }else{ $this->error("操作失败"); } break; case '2': //充值 $out_trade_no = $order_no;//订单号 $order = Db::name('SystemRecharge')->where('number',$out_trade_no)->find(); $buyer_pay_amount = $pay_money;//实际支付金额 if($order['status'] == 0) { if ($order['need_pay'] == $buyer_pay_amount) { Db::startTrans(); try { $order_upd['status'] = 1; $order_upd['pay_time'] = time(); $order_upd['pay_way'] = '苹果内付'; $order_upd['pay_money'] = $buyer_pay_amount; $order_upd['pay_number'] = $out_trade_no; Db::name('SystemRecharge') ->where('number',$out_trade_no) ->update($order_upd); $this -> addMoney($order['mem_id'],$order['money']); Db::commit(); $this->success("购买成功"); } catch (\Exception $e) { Db::rollback(); $msg = $e->getMessage(); $this->error($msg); } }else{ $this->error('金额验证失败'); } }else{ $this->error('订单状态错误'); } break; default: $this->error('订单类型错误'); } } else { $this->error("购买失败".$data['status']); } } /** * 21000 App Store不能读取你提供的JSON对象 * 21002 receipt-data域的数据有问题 * 21003 receipt无法通过验证 * 21004 提供的shared secret不匹配你账号中的shared secret * 21005 receipt服务器当前不可用 * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送 * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务 * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务 */ function acurl($receipt_data, $sandbox = 0) { //小票信息 $POSTFIELDS = array("receipt-data" => $receipt_data); $POSTFIELDS = json_encode($POSTFIELDS); //正式购买地址 沙盒购买地址 $url_buy = "https://buy.itunes.apple.com/verifyReceipt"; $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt"; $url = $sandbox ? $url_sandbox : $url_buy; //简单的curl $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); $result = curl_exec($ch); curl_close($ch); return $result; } }