|
@@ -0,0 +1,159 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace rbridge\youjun;
|
|
|
+
|
|
|
+require_once(BASE_HELPER_PATH . '/rbridge/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/youjun/product.php');
|
|
|
+ }
|
|
|
+
|
|
|
+ $dispatcher = function ($product_num)
|
|
|
+ {
|
|
|
+ global $youjun_product;
|
|
|
+ if(empty($youjun_product)) return [config::DEFAULT_MCHID,[]];
|
|
|
+
|
|
|
+ foreach ($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("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("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("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);
|
|
|
+ }
|
|
|
+}
|