123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace refill\suning;
- require_once(BASE_HELPER_RAPI_PATH . '/suning/config.php');
- use refill;
- use Log;
- class RefillPhone extends refill\IRefillPhone
- {
- public function __construct($cfgs)
- {
- parent::__construct($cfgs);
- }
- public function goods($quality,int $amount,int $card_type,$other)
- {
- [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$other);
- if($goods_id <= 0) return [0,0];
- $store_id = $this->mStoreID;
- $pcode = $other['product_code'];
- $thrid_refill = Model('thrid_refill');
- $product = $thrid_refill->getProviderProduct($store_id,$goods_id,$pcode);
- if(empty($product)) {
- Log::record("cannot find provider's produce where name={$this->mName}, goods_id = {$goods_id} pcode={$pcode}",Log::ERR);
- return [0,0];
- } else {
- return [$goods_id,ncPriceFormat($product['channel_amount'])];
- }
- }
- private function req_params(string $order_sn, string $product_code,$quantity)
- {
- $params['partner'] = config::PARTNER;
- $params['service'] = 'cardSaleAgent';
- $params['format'] = 'json';
- $params['orderTime'] = date("YmdHis",time());
- $params['partnerOrderNo'] = $order_sn;
- $params['productNo'] = $product_code;
- $params['quantity'] = 1;
- $params['amount'] = $quantity * 100;
- return $params;
- }
- private function getProductCode($goods_id,$sys_pcode)
- {
- $thrid_refill = Model('thrid_refill');
- $product = $thrid_refill->getProviderProduct($this->mStoreID,$goods_id,$sys_pcode);
- if (empty($product)) {
- return false;
- } else {
- return $product['channel_code'];
- }
- }
- //直充提单
- public function add($card_no, $card_type, $amount, $params, &$net_errno = 0)
- {
- if(empty($card_no)) {
- return $this->cardkey_add($params,$net_errno);
- } else {
- return $this->direct_add($card_no, $params, $net_errno);
- }
- }
- private function direct_add($card_no, $params, &$net_errno)
- {
- $order_sn = $params['order_sn'];
- $goods_id = intval($params['goods_id']);
- $product_code = $this->getProductCode($goods_id,$params['product_code']);
- Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
- $quantity = intval($params['quantity']);
- $params = $this->req_params($order_sn,$product_code,$quantity);
- $params['userName'] = $card_no;
- $params['bizType'] = '02';
- $sign = $this->sign($params);
- $params['Sign'] = $sign;
- $resp = http_request(config::ORDER_URL, $params, 'GET', false, [], $net_errno);
- if (empty($resp)) {
- return [false, '网络错误', true];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '网络错误', true];
- } elseif ($resp['respCode'] == 00) {
- $notify_data = $this->notify_data_format($resp);
- //可替换延迟回调
- refill\util::push_notify('suning',$notify_data);
- return [true, $resp['orderNo'], false];
- } else {
- return [false, $resp['message'], false];
- }
- }
- }
- private function cardkey_add($params,&$net_errno)
- {
- $order_sn = $params['order_sn'];
- $goods_id = intval($params['goods_id']);
- $product_code = $this->getProductCode($goods_id,$params['product_code']);
- Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
- $quantity = intval($params['quantity']);
- $params = $this->req_params($order_sn,$product_code,$quantity);
- $params['bizType'] = '01';
- $sign = $this->sign($params);
- $params['Sign'] = $sign;
- $resp = http_request(config::CARD_ORDER_URL, $params, 'GET', false, [], $net_errno);
- if (empty($resp)) {
- return [false, '网络错误', true];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '网络错误', true];
- } elseif ($resp['respCode'] == 00) {
- $notify_data = $this->notify_data_format($resp);
- //可替换延迟回调
- refill\util::push_notify('suning',$notify_data);
- return [true, $resp['orderNo'], false];
- } else {
- return [false, $resp['message'], false];
- }
- }
- }
- public function query($refill_info)
- {
- return [false, '苏宁三方下单即回调,无需查询接口'];
- }
- private function sign($params)
- {
- ksort($params);
- $body = "";
- $i = 0;
- foreach ($params as $k => $v) {
- if ($i == 0) {
- $body .= "{$k}={$v}";
- } else {
- $body .= "&{$k}={$v}";
- }
- $i++;
- }
- return bin2hex(hash_hmac("sha1", $body, config::APP_KEY, true));
- }
- private function notify_data_format($params): array
- {
- $data['order_sn'] = $params['partnerOrderNo'];
- $data['cardList'] = $params['cardList'];
- return $data;
- }
- }
|