RefillPhone.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace refill\suning;
  3. require_once(BASE_HELPER_RAPI_PATH . '/suning/config.php');
  4. use refill;
  5. use Log;
  6. class RefillPhone extends refill\IRefillPhone
  7. {
  8. public function __construct($cfgs)
  9. {
  10. parent::__construct($cfgs);
  11. }
  12. public function goods($quality,int $amount,int $card_type,$regin_no,$other)
  13. {
  14. [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$regin_no,$other);
  15. if($goods_id <= 0) return [0,0];
  16. $store_id = $this->mStoreID;
  17. $pcode = $other['product_code'];
  18. $thrid_refill = Model('thrid_refill');
  19. $product = $thrid_refill->getProviderProduct($store_id,$goods_id,$pcode);
  20. if(empty($product)) {
  21. Log::record("cannot find provider's produce where name={$this->mName}, goods_id = {$goods_id} pcode={$pcode}",Log::ERR);
  22. return [0,0];
  23. } else {
  24. return [$goods_id,ncPriceFormat($product['channel_amount'])];
  25. }
  26. }
  27. private function req_params(string $order_sn, string $product_code,$quantity)
  28. {
  29. $params['partner'] = config::PARTNER;
  30. $params['service'] = 'cardSaleAgent';
  31. $params['format'] = 'json';
  32. $params['orderTime'] = date("YmdHis",time());
  33. $params['partnerOrderNo'] = $order_sn;
  34. $params['productNo'] = $product_code;
  35. $params['quantity'] = 1;
  36. $params['amount'] = $quantity * 100;
  37. return $params;
  38. }
  39. private function getProductCode($goods_id,$sys_pcode)
  40. {
  41. $thrid_refill = Model('thrid_refill');
  42. $product = $thrid_refill->getProviderProduct($this->mStoreID,$goods_id,$sys_pcode);
  43. if (empty($product)) {
  44. return false;
  45. } else {
  46. return $product['channel_code'];
  47. }
  48. }
  49. //直充提单
  50. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0)
  51. {
  52. refill\util::send_test($params['order_sn']);
  53. return [true , '',false];
  54. }
  55. private function direct_add($card_no, $params, &$net_errno)
  56. {
  57. $order_sn = $params['order_sn'];
  58. $goods_id = intval($params['goods_id']);
  59. $product_code = $this->getProductCode($goods_id,$params['product_code']);
  60. Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
  61. $quantity = intval($params['quantity']);
  62. $params = $this->req_params($order_sn,$product_code,$quantity);
  63. $params['userName'] = $card_no;
  64. $params['bizType'] = '02';
  65. $sign = $this->sign($params);
  66. $params['sign'] = $sign;
  67. $resp = http_request(config::ORDER_URL, $params, 'GET', false, [], $net_errno);
  68. if (empty($resp)) {
  69. return [false, '网络错误', true];
  70. }
  71. else
  72. {
  73. Log::record($resp, Log::DEBUG);
  74. $resp = json_decode($resp, true);
  75. if (empty($resp)) {
  76. return [false, '网络错误', true];
  77. } elseif ($resp['respCode'] == '00') {
  78. $notify_data = $this->notify_data_format($resp,true);
  79. refill\util::async_notify('suning',$notify_data,1);
  80. return [true, $resp['orderNo'], false];
  81. } else {
  82. return [false, $resp['message'], false];
  83. }
  84. }
  85. }
  86. private function cardkey_add($params,&$net_errno)
  87. {
  88. $order_sn = $params['order_sn'];
  89. $goods_id = intval($params['goods_id']);
  90. $product_code = $this->getProductCode($goods_id,$params['product_code']);
  91. Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
  92. $quantity = intval($params['quantity']);
  93. $params = $this->req_params($order_sn,$product_code,$quantity);
  94. $params['bizType'] = '01';
  95. $sign = $this->sign($params);
  96. $params['sign'] = $sign;
  97. $resp = http_request(config::CARD_ORDER_URL, $params, 'GET', false, [], $net_errno);
  98. if (empty($resp)) {
  99. return [false, '网络错误', true];
  100. }
  101. else
  102. {
  103. Log::record($resp, Log::DEBUG);
  104. $resp = json_decode($resp, true);
  105. if (empty($resp)) {
  106. return [false, '网络错误', true];
  107. } elseif ($resp['respCode'] == '00') {
  108. $notify_data = $this->notify_data_format($resp, false);
  109. refill\util::async_notify('suning', $notify_data, 1);
  110. return [true, $resp['orderNo'], false];
  111. } else {
  112. return [false, $resp['message'], false];
  113. }
  114. }
  115. }
  116. private function rsa_decode($encrypted)
  117. {
  118. $ret = openssl_private_decrypt(base64_decode($encrypted), $decrypted, config::PRIVATE_KEY);
  119. if ($ret) {
  120. $result = json_decode($decrypted, true);
  121. return $result;
  122. } else {
  123. $err = openssl_error_string();
  124. Log::record("suning callback openssl_private_decrypt error={$err}",Log::ERR);
  125. return false;
  126. }
  127. }
  128. public function query($refill_info)
  129. {
  130. return [false, '苏宁三方下单即回调,无需查询接口'];
  131. }
  132. private function sign($params)
  133. {
  134. ksort($params);
  135. $body = "";
  136. $i = 0;
  137. foreach ($params as $k => $v) {
  138. if ($i == 0) {
  139. $body .= "{$k}={$v}";
  140. } else {
  141. $body .= "&{$k}={$v}";
  142. }
  143. $i++;
  144. }
  145. return hash_hmac("sha1", $body, config::APP_KEY);
  146. }
  147. private function notify_data_format($params,$fdirect): array
  148. {
  149. $data['order_sn'] = $params['partnerOrderNo'];
  150. $data['orderNo'] = $params['orderNo'];
  151. $card_list = $params['cardList'];
  152. if(count($card_list) == 1)
  153. {
  154. $item = $params['cardList'][0];
  155. if($fdirect) {
  156. $data['card_info'] = ['type' => 'direct','card_no' => $item['cardNo'],'amount' => $item['amount'],'desc' => $item['cardCatalogName']];
  157. }
  158. else {
  159. $password = $this->rsa_decode($item['password']);
  160. $data['card_info'] = ['type' => 'card_key',
  161. 'card_no' => $item['cardNo'],'amount' => $item['amount'],'password' => $password,
  162. 'desc' => $item['cardCatalogName']];
  163. }
  164. }
  165. else {
  166. $data['card_info'] = [];
  167. }
  168. return $data;
  169. }
  170. }