REAPI/app/channel/service/system/Bluebrother.php

170 lines
5.4 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
use BlueBrothers\Openapi\Api\Client;
use BlueBrothers\Openapi\Api\Constant;
use BlueBrothers\Openapi\Util;
class Bluebrother extends Core
{
# 这几个可以后台设置
protected $host = '';
protected $mid = '';
protected $token = '';
protected $api = '';
protected $itemId = '';
protected $is_prod = false;
# 中石化油卡充值
public function dhcz($param)
{
$check = array
(
'mobile' => 'rechargeAccount',
'cash' => 'cash',
);
$param['accountType'] = 1;
$this->api='recharge/order';
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$result = array();
$result['cash'] = 1;
$result['status'] = 4;
if(isset($data['query_status'])){
$data['status']=$data['query_status'];
}
if ($data['status'] == '01') {
$result['status'] = 2;
} else if($data['status'] == '03') {
$result['status'] = 3;
}else {
$result['status'] = 4;
}
// if(isset($data['cardCode'])&&$data['cardCode']){
// $cardCode=Util::getDecodeCardText($data['cardCode'], $this->token);
// $result['kami'] = array
// (
//// 'cardno' => isset($data['cards'][0]['card_no'])?$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'] = 'success';
$result['data'] = $data;
return $result;
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
if (is_object($data)) {
$array = (array) $data;
$data = $array;
} elseif (is_array($data)) {
$array = $data;
} elseif(is_string($data) && strstr($data, '{')) {
$array = $this->json_decode($data);
} else {
$array['message'] = $data;
}
if (!$array) {
$msg = 'error';
} elseif (isset($array['code']) && $array['code'] == 2000) {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = isset($array['message']) ? $array['message'] : 'error';
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
# 查询接口
public function query($param)
{
$client = new Client($this->mid, $this->token, $this->is_prod, 10);
$response = $client->RechargeQuery($param['order_id']);
$log['type'] = 'query_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$this->assertEquals("0000", $response["code"]);
$array = $response;
$result = array();
$result['status'] = 4;
if ($array['code']=='0000' ) {
if($array['status'] =='01'){
$result['query_status'] = '01';
$result['status'] = 2;
}elseif ($array['state'] == '03'){
$result['query_status'] = '03';
$result['status'] = 3;
}
}
// if(isset($array['data']['cards'])){
// $result['kami']=array(
// 'cardno' => isset($array['data']['cards'][0]['card_no'])?$array['data']['cards'][0]['card_no']:'',
// 'cardpwd' => $array['data']['cards'][0]['card_password'],
// 'expired' => $array['data']['cards'][0]['expired_at'],
// );
// }
return $result;
}
# 提交数据
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'];
$client = new Client($this->mid, $this->token, $this->is_prod, 10);
$request['outTradeNo']= $param['order'];// 合作商系统内部订单号,同一商户下不可重复, 同微信、支付宝的out_trade_no类似
$request['productId'] = $this->getGid($param['cash']);
unset($request['cash']);
$request['number'] = 1;
$request['accountType']=0;
if(isset($param['accountType'])&&$param['accountType']){
$request['accountType']=$param['accountType'];
}
$request['notifyUrl'] = $this->getNotify($param['order'], 1);
$resp = $client->RechargeOrder($request);
$response = $this->response($resp);
$url=$this->host.$this->api;
$channel_order_id = '';
if (isset($response['array']['data']['billId'])) {
$channel_order_id = $response['data']['billId'];
}
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['msg'];
}
}