123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace rbridge\fulu_youjun;
- require_once(BASE_HELPER_PATH . '/rbridge/fulu_youjun/config.php');
- use rbridge\IBridge;
- use refill;
- use Log;
- use StatesHelper;
- class Bridge implements IBridge
- {
- public function add($params)
- {
- if(StatesHelper::fetch_state('bridge')) {
- include (BASE_HELPER_PATH . '/rbridge/fulu_youjun/product.php');
- }
- $dispatcher = function ($product_num)
- {
- global $fulu_youjun_product;
- if(empty($fulu_youjun_product)) return [config::DEFAULT_MCHID,[]];
- foreach ($fulu_youjun_product as $mchid => $product_infos)
- {
- if(array_key_exists($product_num, $product_infos)) {
- $card_types = $product_infos[$product_num];
- return [$mchid,$card_types];
- }
- }
- return [config::DEFAULT_MCHID,[]];
- };
- $params = $this->aes_decrypt($params);
- if (empty($params)) {
- return [false, '数据解密失败'];
- }
- $json_str = json_encode($params);
- Log::record("fulu_youjun add params: {$json_str}", Log::DEBUG);
- $SupProductId = $params['SupProductId'];
- [$mchid,$mch_card_types] = $dispatcher($SupProductId);
- $chargeType = intval($params['OrderType']);
- //订单类型:1话费 2流量 3卡密 4直充,
- if (!in_array($chargeType, [1, 3, 4], true)) {
- return [false, '订单类型不支持'];
- }
- Model('merchant_query')->add_info($mchid, $params['Id'], json_encode($params));
- $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
- $userid = intval($mchinfo['admin_id']);
- $order_time = time();
- $mch_order = $params['Id'];
- $card_no = $params['ChargeAccount'];
- $input = [ 'mchid' => $mchid,
- 'buyer_id' => $userid,
- 'amount' => $params['SupProductFaceValue'],
- 'card_no' => $card_no,
- 'mch_order' => $mch_order,
- 'mch_card_types' => $mch_card_types,
- 'notify_url' => config::MCH_NOTIFY_URL,
- 'order_time' => $order_time
- ];
- refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_QUEUE);
- Model('refill_order')->add_detail($mchid,$mch_order,$order_time,$params,ORDER_STATE_QUEUE);
- $state = refill\util::push_add($input);
- if ($state === true) {
- Log::record("fulu_youjun::Bridge refill::util::push_add success mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG);
- return [true, '提交成功'];
- } else {
- refill\util::del_queue_order($mchid,$mch_order);
- Model('refill_order')->del_detail($mchid,$mch_order);
- Log::record("fulu_youjun::Bridge refill::util::push_add error mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG);
- return [false, '提交失败'];
- }
- }
- public function notify($params)
- {
- $mchid = $params['mchid'];
- $mch_ordersn = $params['order_sn'];
- $body = $this->notify_body($params);
- $userID = config::USER_ID;
- $header = [
- 'Content-Type: application/json',
- "OrderNo: {$mch_ordersn}",
- "UserID: {$userID}",
- ];
- $body = json_encode($body, JSON_UNESCAPED_UNICODE);
- $body = $this->aes_encrypt($body);
- $reqData['PostData'] = $body;
- $reqData = json_encode($reqData, JSON_UNESCAPED_UNICODE);
- $resp = http_post_data(config::ORDER_COMPLETE_URL, $reqData, $header, $net_errno);
- if (empty($resp)) {
- Log::record("回调下游,请求超时 mchid = {$mchid} mch_order = {$mch_ordersn}", Log::ERR);
- return false;
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- Log::record("回调下游,返回数据格式有误 mchid = {$mchid} mch_order = {$mch_ordersn}", Log::ERR);
- return false;
- } elseif ($resp['code'] === '200' && $resp['data'] === true) {
- return true;
- } else {
- Log::record("回调下游,返回失败 mchid = {$mchid} mch_order = {$mch_ordersn} message = {$resp['message']}", Log::ERR);
- return false;
- }
- }
- }
- private function notify_body($params)
- {
- $success = $params['state'] == 'SUCCESS';
- $body = [
- "OrderNo" => $params['order_sn'],
- "Status" => $success ? 1 : 2,
- "Description" => '',
- "RealCost" => "",
- "SubstituteAccount" => "",
- "ChannelAccountId" => "",
- "InventoryId" => "",
- "OperatorSerialNumber" => ""
- ];
- if($body['Status'] === 1) {
- $body['OperatorSerialNumber'] = $params['official_sn'];
- }
- return $body;
- }
- private function aes_encrypt($data) {
- $str_padded = $data;
- if (strlen($str_padded) % 16) {
- $str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0");
- }
- $data = openssl_encrypt($str_padded, 'AES-128-CBC', config::KEY, OPENSSL_NO_PADDING, config::AES_IV);
- return base64_encode($data);
- }
- //AES-128-ECB解密 data 字符串
- private function aes_decrypt($data) {
- $encrypted = base64_decode($data);
- $params = openssl_decrypt($encrypted, 'AES-128-CBC', config::KEY, OPENSSL_NO_PADDING, config::AES_IV);
- return json_decode(trim($params), true);
- }
- }
|