REAPI/app/channel/service/system/Maidanglao.php
2024-09-29 15:43:18 +08:00

208 lines
6.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\channel\service\system;
class Maidanglao extends Core
{
protected $host = '';
protected $mid = '';
protected $token = '';
protected $api = '';
protected $merchant_id='';//麦当劳商户idappid为应用ID即系统内的mid
protected $version='1.0';
# 通用卡密购买
public function cardbuy($param)
{
// microtime(true) * 1000;
$check = array
(
'account' => 'customerMobileNo',
'cash'=>'cash'
);
$this->api='coupon/order';
return $this->submit($param, $check);
}
public function mhql_cardbuy($param){
$check = array
(
'account' => 'recharge_account',
'cash'=>'cash'
);
$this->api='api/buy';
return $this->submit($param, $check);
}
public function jdhy_cardbuy($param){
$check = array
(
'account' => 'recharge_account',
'cash'=>'cash'
);
$this->api='api/buy';
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$result = array();
$result['cash'] = 1;
$result['status'] = 4;
if(isset($data['query_status'])){
if ($data['query_status'] == 200) {
$result['status'] = 2;
} else if($data['query_status'] == 500) {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
}else{
if ($data['state'] == 200) {
$result['status'] = 2;
} else if($data['state'] == 500) {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
}
if(isset($data['cards'])&&$data['cards']){
$result['kami'] = array
(
'cardno' => $data['cards'][0]['card_no'],
'cardpwd' => $data['cards'][0]['card_password'],
'expired' => isset($data['cards'][0]['expired_at'])?$data['cards'][0]['expired_at']:'',
// 'value' => $array['data']['count_money'],
);
}
if(isset($data['kami'])&&$data['kami']){
$result['kami']=$data['kami'];
}
$result['yes'] = 'ok';
$result['data'] = $data;
return $result;
}
public function query($order){
$header=array(
'AppId'=>$this->mid,
'MerchantId'=>$this->merchant_id,
'Timestamp'=>microtime(true) * 1000,
'Version'=>$this->version
);
$request['orderId'] = $order['order_id'];
$header['sign'] = $this->_sign($request,$header);
$url = $this->host . 'coupon/order/codes';
$response = $this->curl('get', $url, $request,true,$header);
$log['type'] = 'query_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($response,true);
$result = array();
$result['status'] = 4;
if(isset($array['coupons'])&&$array['coupons']){
}
if ($array['code']=='ok' ) {
if($array['state'] ==200){
$result['query_status'] = 200;
$result['status'] = 2;
}elseif ($array['state'] == 500){
$result['query_status'] = 500;
$result['status'] = 3;
}
}
if(isset($array['data']['cards'])){
$result['kami']=array(
'cardno' => $array['data']['cards'][0]['card_no'],
'cardpwd' => $array['data']['cards'][0]['card_password'],
'expired' => $array['data']['cards'][0]['expired_at'],
);
}
return $result;
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array['orderId'])) {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = isset($array['message']) ? $array['message'] : 'error';
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
# 提交数据
private function submit($param, $check)
{
$param = $this->param($param, $check);
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
return $param;
}
$request = $param['detail'];
$header=array(
'AppId'=>$this->mid,
'MerchantId'=>$this->merchant_id,
'Timestamp'=>microtime(true) * 1000,
'Version'=>$this->version
);
$request['outRequestNo']=$param['order'];
$request['orderGoods']=array(
'count'=>1,
'skuid'=>$this->getGid($param['cash'])
);
unset($request['cash']);
$key=$this->token;
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$cipher = 'aes-256-cbc';
$request['customerMobileNo']=openssl_encrypt($request['customerMobileNo'], $cipher, $key, 0, $iv);
$header['sign']=$this->_sign($request,$header);
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request,true,$header);
$response = $this->response($response);
$channel_order_id = '';
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['msg'];
}
private function _sign($request,$header)
{
unset($request['cash']);
$data=array_merge($header,$request);
ksort($data);
$str='';
foreach ($data as $k => $v) {
$str .= $v . '=' . $data[$v] . '&';
}
$str.='key='.$this->token;
$str=strtoupper(md5($str));
return $str;
}
}