更新Code
为确保转赠后的安全性,微信允许自定义code的商户对已下发的code进行更改。
注:为避免用户疑惑,建议仅在发生转赠行为后(发生转赠后,微信会通过事件推送的方式告知商户被转赠的卡券code)对用户的code进行更改。
@param $code
@param $card_id
@param $new_code
@return bool
@throws HttpException
192084
public function updateCode($code, $card_id, $new_code)
    {
        if (empty($code) || empty($card_id) || $new_code) {
            return false;
        }

        $result = $this->httpPost(static::WECHAT_CARD_CODE_UPDATE_URL . 'access_token=' . $this->getAccessToken(), [
            'code' => $code,
            'card_id' => $card_id,
            'new_code' => $new_code
        ]);

        return isset($result['errmsg']) && $result['errmsg'] === 'ok' ? true : false;
    }
571393
public function setReceptionCode($newCode, $userId)
    {
        $oldCode = $this->respondent->getReceptionCode();
        $code    = $this->respondent->setReceptionCode($newCode);

        // Is the respondent really removed
        if ($code->isSuccess()) {
            $this->addMessage($this->_('Respondent restored.'));

            if ($this->formData['restore_tracks']) {
                $count = $this->respondent->restoreTracks($oldCode, $code);
                
                $this->addMessage(sprintf($this->plural('Restored %d track.', 'Restored %d tracks.', $count), $count));
            }

        } else {
            // Perform actual save, but not simple stop codes.
            if ($code->isForRespondents()) {
                $this->addMessage($this->_('Respondent deleted.'));
                $this->afterSaveRouteKeys = false;
                $this->resetRoute         = true;
                $this->routeAction        = 'index';
            } else {
                // Just a stop code
                $this->addMessage(sprintf($this->plural('Stopped %d track.', 'Stopped %d tracks.', $count), $count));
            }
        }

        return 1;
    }
1329109
public function deposit($card_id, array $code)
    {
        $url = "https://api.weixin.qq.com/card/code/deposit?access_token=ACCESS_TOKEN";
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->httpPostForJson($url, ['card_id' => $card_id, 'code' => $code]);
    }
145575
public function setReceptionCode($newCode)
    {
        return $this->model->setReceptionCode(
                $this->getPatientNumber(),
                $this->getOrganizationId(),
                $newCode,
                $this->getId(),
                $this->getReceptionCode()
                );
    }
1048474
public function get(string $code, string $cardId = '', bool $checkConsume = true)
    {
        $params = [
            'code' => $code,
            'check_consume' => $checkConsume,
            'card_id' => $cardId,
        ];

        return $this->httpPostJson('card/code/get', $params);
    }
302303
public function getCard($card_id)
    {
        if (empty($card_id)) {
            return false;
        }
        $result = $this->httpRaw(static::WECHAT_GET_CARD_URL . 'access_token=' . $this->getAccessToken(), Json::encode([
            'card_id' => $card_id
        ]));

        return isset($result['card']) ? $result['card'] : false;
    }
760855
public function wxCardPackage(array $card_list, $timestamp = null, $api_ticket = null)
    {
        if (!is_array($card_list)) {
            $this->setError('参数缺失');

            return false;
        }

        if (empty($timestamp) || $timestamp == '') {
            $timestamp = time();
        }

        if (empty($api_ticket) || $api_ticket == '') {
            $api_ticket = $this->cardApiTicket();
        }

        $resultArray = [];
        foreach ($card_list as $key => $value) {
            if (empty($value['code']) || !isset($value['code'])) {
                $value['code'] = '';
            }
            if (empty($value['openid']) || !isset($value['openid'])) {
                $value['openid'] = '';
            }
            $arrays = [$api_ticket, $timestamp, $value['card_id'], $value['code'], $value['openid']];
            sort($arrays, SORT_STRING);
            $string = sha1(implode($arrays));

            $resultArray['cardList'][$key]['cardId']            = $value['card_id'];
            $resultArray['cardList'][$key]['cardExt']['code']   = $value['code'];
            $resultArray['cardList'][$key]['cardExt']['openid'] = $value['openid'];

            $resultArray['cardList'][$key]['cardExt']['timestamp'] = $timestamp;
            $resultArray['cardList'][$key]['cardExt']['signature'] = $string;

            if (!empty($value['outer_id'])) {
                $resultArray['cardList'][$key]['cardExt']['outer_id'] = $value['outer_id'];
            }
            $resultArray['cardList'][$key]['cardExt'] = json_encode($resultArray['cardList'][$key]['cardExt']);
        }
        $resultJson = json_encode($resultArray);

        return $resultJson;
    }
250206
public function actionConnect($code)
    {

        $account = $this->finder->findAccount()->byCode($code)->one();

        if ($account === null || $account->getIsConnected()) {
            throw new NotFoundHttpException();
        }

        /** @var User $user */
        $user = \Yii::createObject([
            'class'    => User::className(),
            'scenario' => 'connect',
            'email'    => $account->email,
        ]);
        $profile = new Profile();

        $event = $this->getConnectEvent($account, $user);

        $this->trigger(self::EVENT_BEFORE_CONNECT, $event);

        if ($user->load(\Yii::$app->request->post())) {
            if($profile->load(\Yii::$app->request->post()) && $profile->validate()) {
                $user->username = $user->email;
                $user->password = Password::generate(8);
                if($profile->user_id = $user->register(Url::to(['/user/login', 'language' => Language::getCurrent()->lang_id], true))) {
                    $profile->save();

                    $this->trigger(self::EVENT_AFTER_REGISTER, new UserRegistrationEvent([
                        'id' => $profile->user_id
                    ]));

                    $account->connect($user);
                    $this->trigger(self::EVENT_AFTER_CONNECT, $event);
                    /*\Yii::$app->user->login($user, $this->module->rememberFor);
                    return $this->goBack();*/


                    Yii::$app->session->setFlash(
                        'info',
                        Yii::t(
                            'user',
                            'Your account has been created and a message with further instructions has been sent to your email'
                        )
                    );

                    return $this->render('/message', [
                        'title'  => \Yii::t('user', 'Your account has been created'),
                        'module' => $this->module,
                        'profile' => $profile
                    ]);
                }
            }
        }

        return $this->render('connect', [
            'model'   => $user,
            'account' => $account,
            'profile' => $profile,
        ]);
    }
1237482
public function setHttpCode($http_code) {
        if(!in_array($http_code, self::$VALID_HTTP))
            throw new \InvalidArgumentException('Invalid HTTP code');
        $this->http_code= $http_code;
    }
392489
public function attemptMobileChange($code)
    {
        /** @var Token $token */
        $token = Token::find()->where([
            'user_id' => $this->id,
            'code' => $code
        ])->andWhere(['in', 'type', [Token::TYPE_CONFIRM_NEW_MOBILE, Token::TYPE_CONFIRM_OLD_MOBILE]])->one();
        if (empty($this->unconfirmed_mobile) || $token === null || $token->isExpired) {
            Yii::$app->session->setFlash('danger', Yii::t('user', 'Your confirmation token is invalid or expired'));
        } else {
            $token->delete();
            if (empty($this->unconfirmed_mobile)) {
                Yii::$app->session->setFlash('danger', Yii::t('user', 'An error occurred processing your request'));
            } elseif (static::find()->where(['mobile' => $this->unconfirmed_mobile])->exists() == false) {
                if ($this->getSetting('mobileChangeStrategy') == Settings::STRATEGY_SECURE) {
                    switch ($token->type) {
                        case Token::TYPE_CONFIRM_NEW_MOBILE:
                            $this->flags |= self::NEW_MOBILE_CONFIRMED;
                            Yii::$app->session->setFlash('success', Yii::t('user', 'Awesome, almost there. Now you need to input the confirmation code sent to your old mobile'));
                            break;
                        case Token::TYPE_CONFIRM_OLD_MOBILE:
                            $this->flags |= self::OLD_MOBILE_CONFIRMED;
                            Yii::$app->session->setFlash('success', Yii::t('user', 'Awesome, almost there. Now you need to input the confirmation code sent to your new mobile'));
                            break;
                    }
                }
                if ($this->getSetting('mobileChangeStrategy') == Settings::STRATEGY_DEFAULT
                    || ($this->flags & self::NEW_MOBILE_CONFIRMED && $this->flags & self::OLD_MOBILE_CONFIRMED)) {
                    $this->mobile = $this->unconfirmed_mobile;
                    $this->unconfirmed_mobile = null;
                    Yii::$app->session->setFlash('success', Yii::t('user', 'Your mobile address has been changed'));
                }
                $this->save(false);
            }
        }
    }
126378
public function updateCode($code, $new_code, $card_id = null)
    {
        $data = ['code' => $code, 'new_code' => $new_code];
        is_null($card_id) || $data['card_id'] = $card_id;
        $url = "https://api.weixin.qq.com/card/code/update?access_token=ACCESS_TOKEN";
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->httpPostForJson($url, $data);
    }
192058
public function unAvailable($code, $card_id, $reason = null)
    {
        $data = ['code' => $code, 'card_id' => $card_id];
        is_null($reason) || $data['reason'] = $reason;
        $url = "https://api.weixin.qq.com/card/code/unavailable?access_token=ACCESS_TOKEN";
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->httpPostForJson($url, $data);
    }
1342825
public function cardInfo($number, $expirationDate, $code)
    {
        $this->setArgument('card_number', $number);
        $this->setArgument('card_expiration_date', $expirationDate);
        return $this->setArgument('card_code', $code);
    }
580444
public function getNewsHtml($card_id)
    {
        $url = "https://api.weixin.qq.com/card/mpnews/gethtml?access_token=ACCESS_TOKEN";
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->httpPostForJson($url, ['card_id' => $card_id]);
    }
1253610
private function newCard($securityCode, $brand)
    {
        $card = new CreditCard();
        $card->setSecurityCode($securityCode);
        $card->setBrand($brand);

        return $card;
    }
995522
public function customWxcard($card_id = '', $card_ext = [], $openid, $kf_account = '')
    {
        $queryStr                       = [];
        $queryStr['touser']             = $openid;
        $queryStr['msgtype']            = 'wxcard';
        $queryStr['wxcard']['card_id']  = $card_id;
        $queryStr['wxcard']['card_ext'] = $card_ext;

        $queryStr['customservice']['kf_account'] = $kf_account;

        $this->module = 'message';

        $res = $this->_post('custom/send', $queryStr);

        return $res;
    }
1013486
public function decryptCardCode($encrypt_code) {
        $data = array(
            'encrypt_code' => $encrypt_code,
        );
        if (!$this->access_token && !$this->checkAuth()) return false;
        $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . 'access_token=' . $this->access_token, self::json_encode($data));
        if ($result) {
            $json = json_decode($result, true);
            if (!$json || !empty($json['errcode'])) {
                $this->errCode = $json['errcode'];
                $this->errMsg  = $json['errmsg'];
                return false;
            }
            return $json;
        }
        return false;
    }