123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?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,$regin_no,$other)
- {
- [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$regin_no,$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)
- {
- refill\util::send_test($params['order_sn']);
- return [true , '',false];
- }
- 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,true);
- refill\util::async_notify('suning',$notify_data,1);
- 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, false);
- refill\util::async_notify('suning', $notify_data, 1);
- return [true, $resp['orderNo'], false];
- } else {
- return [false, $resp['message'], false];
- }
- }
- }
- private function rsa_decode($encrypted)
- {
- $ret = openssl_private_decrypt(base64_decode($encrypted), $decrypted, config::PRIVATE_KEY);
- if ($ret) {
- $result = json_decode($decrypted, true);
- return $result;
- } else {
- $err = openssl_error_string();
- Log::record("suning callback openssl_private_decrypt error={$err}",Log::ERR);
- return 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 hash_hmac("sha1", $body, config::APP_KEY);
- }
- private function notify_data_format($params,$fdirect): array
- {
- $data['order_sn'] = $params['partnerOrderNo'];
- $data['orderNo'] = $params['orderNo'];
- $card_list = $params['cardList'];
- if(count($card_list) == 1)
- {
- $item = $params['cardList'][0];
- if($fdirect) {
- $data['card_info'] = ['type' => 'direct','card_no' => $item['cardNo'],'amount' => $item['amount'],'desc' => $item['cardCatalogName']];
- }
- else {
- $password = $this->rsa_decode($item['password']);
- $data['card_info'] = ['type' => 'card_key',
- 'card_no' => $item['cardNo'],'amount' => $item['amount'],'password' => $password,
- 'desc' => $item['cardCatalogName']];
- }
- }
- else {
- $data['card_info'] = [];
- }
- return $data;
- }
- }
|