public function couponNotify(){
$xml = file_get_contents("php://input");
$arr = $this->xmltoarray($xml);
if($arr['result_code']=="SUCCESS" && $arr['return_code']=="SUCCESS"){
$orderNum = $arr['out_trade_no'];
//取出userid
$userId = substr($orderNum,'0',strrpos($orderNum,"u"));//前面
//取出couponid
$couponId = substr($orderNum,strpos($orderNum,'u')+1,strpos($orderNum,'s')-3);
//取出Guid
//$gradeUid = substr($orderNum,strpos($orderNum,'s')+1,strpos($orderNum,'g')-7);
$preg= '/s(.*?)g/';
preg_match_all($preg,$orderNum,$res);
$gradeUid = $res[1][0];
if($gradeUid != null){
$this->createProfit($gradeUid,$userId,3);//3.下单加100 1级10 二级20
}
$data = array(
'transaction_id' => $arr['transaction_id'],
'out_trade_no'=>$arr['out_trade_no'],
'status'=>1
);
$isEmpty = DB::table('my_coupon')->where('user_id',$userId)->where('coupon_id',$couponId)->where('type',3)->get();
if($isEmpty->isEmpty()){
DB::beginTransaction(); //开启事务
$res1 = DB::table('zs_order')->where('user_id',$userId)->where('coupon_id',$couponId)->where('status',0)->update($data);
$data1 = ['user_id'=>$userId,'coupon_id'=>$couponId,'type'=>3,'code'=>createCode(),'created_at'=>date("Y-m-d H:i:s")];
$res2 = DB::table('my_coupon')->insert($data1);
if($res1 && $res2){
DB::commit(); //提交
}else{
DB::rollback();//回滚
}
}
return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
}
}
function xmltoarray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
网友评论