Q币直充-迅银渠道商(php 面向对象类)

  Q币直充的一个类实现 

1
<?php 2 /** 3 * DEC : 迅银Q币直充 4 * User: David Wang 5 * Time: 2018/5/24 下午1:09 6 */ 7 8 namespace goods\classes; 9 10 class XyDelivery extends Delivery { 11 private $cpid ; 12 private $key ; 13 private $request_url = 'http://gw.api.xunyin.com/Pay'; 14 private $notify_url; 15 public $order_id; 16 public $service_ip; 17 18 public function __construct() { 19 $this->cpid = ‘商户号’; 20 $this->key = ‘迅银分配的key’; 21 $this->request_url = ‘请求地址’; 22 $this->notify_url = ‘回调地址’; 23 $this->service_ip = ‘服务端IP’; 24 } 25 26 public function getId() { 27 return 'xunyin'; 28 } 29 30 public function getName() { 31 return '迅银Q币直充'; 32 } 33 34 public function run($params) { 35 if (!$params['amount'] || !$params['account']) { 36 return false; 37 } 38 $data['cpid'] = $this->cpid; 39 //金额不同商品id不一样 1-5档 6-2000档 40 if ($params['amount'] < 6) { 41 $goods_id = 813937; 42 } else { 43 $goods_id = 800009; 44 } 45 $data['gamegoodid'] = $goods_id; 46 $data['createtime'] = date('YmdHis', time()); 47 $data['account'] = $params['account']; 48 $data['orderid'] = $this->cr_order_no('ruanqin'); 49 $data['buynum'] = $params['amount']; 50 $data['buyerIp'] = $this->service_ip; 51 $data['returnurl'] = $this->notify_url; 52 $data['sign'] = $this->createSign($data, 'pay'); 53 $request = $this->request_url . '?' . $this->array_to_key($data); 54 $this->order_id = $data['orderid']; 55 56 //这里curl get请求渠道商接口 57 $Curl = new Curlient(); 58 $res = $Curl->request($request)->text(); 59 60 $req = $this->xml_to_data($res); 61 if ($req['Code'] == '0000' || $req['Code'] == '8888') { 62 $this->order_id = $data['orderid']; 63 64 return ['error' => 0, 'msg' => 'ok']; 65 } else { 66 return ['error' => 1, 'msg' => $req['msg']]; 67 } 68 69 } 70 71 /** 72 * @param array $channel 73 * 74 * @return array 75 */ 76 public static function get_deliver_channel($channel) { 77 $channel['xunyin'] = new self(); 78 79 return $channel; 80 } 81 82 public function canEnable() { 83 return true; 84 } 85 86 public function onCallback() { 87 $content = @file_get_contents('php://input'); 88 if (!$content) { 89 return false; 90 } 91 $arr = $this->string_to_array($content); 92 $req['id'] = $arr['id']; 93 $req['userid'] = $arr['userid']; 94 $req['status'] = $arr['status']; 95 $req['code'] = $arr['code']; 96 $sign = $this->createSign($req); 97 if ($sign == $arr['sign']) { 98 $deliverModel = new GoodsDeliveryRecord(); 99 $deliver_info = $deliverModel->get(['order_id' => $arr['id']])[0]; 100 if($deliver_info){ 101 if ($arr['status'] == 'SUCCESS' && $arr['code'] = '8888') { 102 //回调成功 做自己的业务处理吧 boy 103 return 'ok'; 104 }else{ 105 $err_data['note'] = $this->errorCode($arr['code']); 106 return $err_data;//错误代码 do somthing 107 } 108 } 109 } 110 111 return false; 112 } 113 114 private function string_to_array($content) { 115 $arr = explode('&', $content); 116 117 $data = []; 118 foreach ($arr as $item) { 119 $it_arr = explode('=', $item); 120 $data[ $it_arr[0] ] = $it_arr[1]; 121 } 122 123 return $data; 124 } 125 126 /** 127 * 生成订单编号 128 * 129 * @param string $prefix 前缀 130 * 131 * @return string 132 */ 133 protected function cr_order_no($prefix = '') { 134 return $prefix . date('YmdHis') . substr(microtime(), 2, 6); 135 } 136 137 private function array_to_key($params) { 138 $string = ''; 139 if (!empty($params)) { 140 $array = array(); 141 foreach ($params as $key => $value) { 142 $array[] = $key . '=' . $value; 143 } 144 $string = implode("&", $array); 145 } 146 147 return $string; 148 } 149 150 /** 151 * @param array $params 152 * 153 * @return string 154 */ 155 private function createSign($params, $type = '') { 156 //这家的数据不用排序 按照他们的固定排序就行了 屌 157 if ($type) { 158 $data['cpid'] = $params['cpid']; 159 $data['gamegoodid'] = $params['gamegoodid']; 160 $data['createtime'] = $params['createtime']; 161 $data['account'] = $params['account']; 162 $data['orderid'] = $params['orderid']; 163 $data['buynum'] = $params['buynum']; 164 $params = $data; 165 } 166 $string = $this->array_to_key($params); 167 168 return strtolower(md5($string . $this->key)); 169 } 170 171 /** 172 * 将xml转为array 173 * 174 * @param string $xml 175 * return array 176 */ 177 public function xml_to_data($xml) { 178 if (!$xml) { 179 return false; 180 } 181 //将XML转为array 182 //禁止引用外部xml实体 183 libxml_disable_entity_loader(true); 184 $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); 185 186 return $data; 187 } 188 189 private function errorCode($code) { 190 $err_arr = ['8019' => '充值的金额有误', '8020' => '充值帐号有误', '8021' => '可疑订单,需要人工确认', '8030' => '充值失败', '9099' => '负利润报警', '8012' => '余额不足']; 191 192 return $err_arr[ $code ]; 193 } 194 195 }

 

posted @ 2018-05-26 16:34  ruanqin  阅读(535)  评论(0编辑  收藏  举报