Bridge.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace rbridge\fulu_youjun;
  3. require_once(BASE_HELPER_PATH . '/rbridge/fulu_youjun/config.php');
  4. use rbridge\IBridge;
  5. use refill;
  6. use Log;
  7. use StatesHelper;
  8. class Bridge implements IBridge
  9. {
  10. public function add($params)
  11. {
  12. if(StatesHelper::fetch_state('bridge')) {
  13. include (BASE_HELPER_PATH . '/rbridge/fulu_youjun/product.php');
  14. }
  15. $dispatcher = function ($product_num)
  16. {
  17. global $fulu_youjun_product;
  18. if(empty($fulu_youjun_product)) return [config::DEFAULT_MCHID,[]];
  19. foreach ($fulu_youjun_product as $mchid => $product_infos)
  20. {
  21. if(array_key_exists($product_num, $product_infos)) {
  22. $card_types = $product_infos[$product_num];
  23. return [$mchid,$card_types];
  24. }
  25. }
  26. return [config::DEFAULT_MCHID,[]];
  27. };
  28. $params = $this->aes_decrypt($params);
  29. if (empty($params)) {
  30. return [false, '数据解密失败'];
  31. }
  32. $json_str = json_encode($params);
  33. Log::record("fulu_youjun add params: {$json_str}", Log::DEBUG);
  34. $SupProductId = $params['SupProductId'];
  35. [$mchid,$mch_card_types] = $dispatcher($SupProductId);
  36. $chargeType = intval($params['OrderType']);
  37. //订单类型:1话费 2流量 3卡密 4直充,
  38. if (!in_array($chargeType, [1, 3, 4], true)) {
  39. return [false, '订单类型不支持'];
  40. }
  41. Model('merchant_query')->add_info($mchid, $params['Id'], json_encode($params));
  42. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  43. $userid = intval($mchinfo['admin_id']);
  44. $order_time = time();
  45. $mch_order = $params['Id'];
  46. $card_no = $params['ChargeAccount'];
  47. $input = [ 'mchid' => $mchid,
  48. 'buyer_id' => $userid,
  49. 'amount' => $params['SupProductFaceValue'],
  50. 'card_no' => $card_no,
  51. 'mch_order' => $mch_order,
  52. 'mch_card_types' => $mch_card_types,
  53. 'notify_url' => config::MCH_NOTIFY_URL,
  54. 'order_time' => $order_time
  55. ];
  56. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_QUEUE);
  57. Model('refill_order')->add_detail($mchid,$mch_order,$order_time,$params,ORDER_STATE_QUEUE);
  58. $state = refill\util::push_add($input);
  59. if ($state === true) {
  60. Log::record("fulu_youjun::Bridge refill::util::push_add success mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG);
  61. return [true, '提交成功'];
  62. } else {
  63. refill\util::del_queue_order($mchid,$mch_order);
  64. Model('refill_order')->del_detail($mchid,$mch_order);
  65. Log::record("fulu_youjun::Bridge refill::util::push_add error mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG);
  66. return [false, '提交失败'];
  67. }
  68. }
  69. public function notify($params)
  70. {
  71. $mchid = $params['mchid'];
  72. $mch_ordersn = $params['order_sn'];
  73. $body = $this->notify_body($params);
  74. $userID = config::USER_ID;
  75. $header = [
  76. 'Content-Type: application/json',
  77. "OrderNo: {$mch_ordersn}",
  78. "UserID: {$userID}",
  79. ];
  80. $body = json_encode($body, JSON_UNESCAPED_UNICODE);
  81. $body = $this->aes_encrypt($body);
  82. $reqData['PostData'] = $body;
  83. $reqData = json_encode($reqData, JSON_UNESCAPED_UNICODE);
  84. $resp = http_post_data(config::ORDER_COMPLETE_URL, $reqData, $header, $net_errno);
  85. if (empty($resp)) {
  86. Log::record("回调下游,请求超时 mchid = {$mchid} mch_order = {$mch_ordersn}", Log::ERR);
  87. return false;
  88. }
  89. else
  90. {
  91. Log::record($resp, Log::DEBUG);
  92. $resp = json_decode($resp, true);
  93. if (empty($resp)) {
  94. Log::record("回调下游,返回数据格式有误 mchid = {$mchid} mch_order = {$mch_ordersn}", Log::ERR);
  95. return false;
  96. } elseif ($resp['code'] === '200' && $resp['data'] === true) {
  97. return true;
  98. } else {
  99. Log::record("回调下游,返回失败 mchid = {$mchid} mch_order = {$mch_ordersn} message = {$resp['message']}", Log::ERR);
  100. return false;
  101. }
  102. }
  103. }
  104. private function notify_body($params)
  105. {
  106. $success = $params['state'] == 'SUCCESS';
  107. $body = [
  108. "OrderNo" => $params['order_sn'],
  109. "Status" => $success ? 1 : 2,
  110. "Description" => '',
  111. "RealCost" => "",
  112. "SubstituteAccount" => "",
  113. "ChannelAccountId" => "",
  114. "InventoryId" => "",
  115. "OperatorSerialNumber" => ""
  116. ];
  117. if($body['Status'] === 1) {
  118. $body['OperatorSerialNumber'] = $params['official_sn'];
  119. }
  120. return $body;
  121. }
  122. private function aes_encrypt($data) {
  123. $str_padded = $data;
  124. if (strlen($str_padded) % 16) {
  125. $str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0");
  126. }
  127. $data = openssl_encrypt($str_padded, 'AES-128-CBC', config::KEY, OPENSSL_NO_PADDING, config::AES_IV);
  128. return base64_encode($data);
  129. }
  130. //AES-128-ECB解密 data 字符串
  131. private function aes_decrypt($data) {
  132. $encrypted = base64_decode($data);
  133. $params = openssl_decrypt($encrypted, 'AES-128-CBC', config::KEY, OPENSSL_NO_PADDING, config::AES_IV);
  134. return json_decode(trim($params), true);
  135. }
  136. }