RefillPhone.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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,$other)
  13. {
  14. [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$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. if(empty($card_no)) {
  53. return $this->cardkey_add($params,$net_errno);
  54. } else {
  55. return $this->direct_add($card_no, $params, $net_errno);
  56. }
  57. }
  58. private function direct_add($card_no, $params, &$net_errno)
  59. {
  60. $order_sn = $params['order_sn'];
  61. $goods_id = intval($params['goods_id']);
  62. $product_code = $this->getProductCode($goods_id,$params['product_code']);
  63. Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
  64. $quantity = intval($params['quantity']);
  65. $params = $this->req_params($order_sn,$product_code,$quantity);
  66. $params['userName'] = $card_no;
  67. $params['bizType'] = '02';
  68. $sign = $this->sign($params);
  69. $params['Sign'] = $sign;
  70. $resp = http_request(config::ORDER_URL, $params, 'GET', false, [], $net_errno);
  71. if (empty($resp)) {
  72. return [false, '网络错误', true];
  73. }
  74. else
  75. {
  76. Log::record($resp, Log::DEBUG);
  77. $resp = json_decode($resp, true);
  78. if (empty($resp)) {
  79. return [false, '网络错误', true];
  80. } elseif ($resp['respCode'] == 00) {
  81. $notify_data = $this->notify_data_format($resp);
  82. //可替换延迟回调
  83. refill\util::push_notify('suning',$notify_data);
  84. return [true, $resp['orderNo'], false];
  85. } else {
  86. return [false, $resp['message'], false];
  87. }
  88. }
  89. }
  90. private function cardkey_add($params,&$net_errno)
  91. {
  92. $order_sn = $params['order_sn'];
  93. $goods_id = intval($params['goods_id']);
  94. $product_code = $this->getProductCode($goods_id,$params['product_code']);
  95. Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
  96. $quantity = intval($params['quantity']);
  97. $params = $this->req_params($order_sn,$product_code,$quantity);
  98. $params['bizType'] = '01';
  99. $sign = $this->sign($params);
  100. $params['Sign'] = $sign;
  101. $resp = http_request(config::CARD_ORDER_URL, $params, 'GET', false, [], $net_errno);
  102. if (empty($resp)) {
  103. return [false, '网络错误', true];
  104. }
  105. else
  106. {
  107. Log::record($resp, Log::DEBUG);
  108. $resp = json_decode($resp, true);
  109. if (empty($resp)) {
  110. return [false, '网络错误', true];
  111. } elseif ($resp['respCode'] == 00) {
  112. $notify_data = $this->notify_data_format($resp);
  113. //可替换延迟回调
  114. refill\util::push_notify('suning',$notify_data);
  115. return [true, $resp['orderNo'], false];
  116. } else {
  117. return [false, $resp['message'], false];
  118. }
  119. }
  120. }
  121. public function query($refill_info)
  122. {
  123. return [false, '苏宁三方下单即回调,无需查询接口'];
  124. }
  125. private function sign($params)
  126. {
  127. ksort($params);
  128. $body = "";
  129. $i = 0;
  130. foreach ($params as $k => $v) {
  131. if ($i == 0) {
  132. $body .= "{$k}={$v}";
  133. } else {
  134. $body .= "&{$k}={$v}";
  135. }
  136. $i++;
  137. }
  138. return bin2hex(hash_hmac("sha1", $body, config::APP_KEY, true));
  139. }
  140. private function notify_data_format($params): array
  141. {
  142. $data['order_sn'] = $params['partnerOrderNo'];
  143. $data['cardList'] = $params['cardList'];
  144. return $data;
  145. }
  146. }