/** * 验证订单消息是否合法, * 不合法返回false, 合法返回订单信息详情 * * @return bool|Bag */ public function verify() { if (version_compare(PHP_VERSION, '5.6.0', '<')) { if (!empty($GLOBALS['HTTP_RAW_POST_DATA'])) { $xmlInput = $GLOBALS['HTTP_RAW_POST_DATA']; } else { $xmlInput = file_get_contents('php://input'); } } else { $xmlInput = file_get_contents('php://input'); } if (empty($xmlInput)) { return false; } $input = XML::parse($xmlInput); if (empty($input) || empty($input['sign'])) { return false; } $sign = $input['sign']; unset($input['sign']); $signGenerator = new SignGenerator($input); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->mchKey; }); if ($sign !== $signGenerator->getResult()) { return false; } return $this->transaction = new Bag($input); }
/** * 生成配置 * * @return array * @throws Payment\Exception */ private function generateConfig() { $response = $this->unifiedOrder->getResponse(); $business = $this->unifiedOrder->getBusiness(); $config = array('appId' => $business->appid, 'timeStamp' => (string) time(), 'nonceStr' => $response['nonce_str'], 'package' => 'prepay_id=' . $response['prepay_id'], 'signType' => 'MD5'); $signGenerator = new SignGenerator($config); $signGenerator->onSortAfter(function (SignGenerator $that) use($business) { $that->key = $business->mch_key; }); $config['paySign'] = $signGenerator->getResult(); return $config; }
/** * 获取订单结果 * * @param string $order_id 商户订单ID * @param bool|false $force 是否忽略缓存强制更新 * @return Bag * @throws Exception * @throws \Overtrue\Wechat\Exception */ public function getTransaction($order_id, $force = false) { $params = array(); $params['appid'] = $this->appId; $params['mch_id'] = $this->mchId; $params['out_trade_no'] = $order_id; $params['nonce_str'] = md5(uniqid(microtime())); $signGenerator = new SignGenerator($params); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->mchKey; }); $params['sign'] = $signGenerator->getResult(); $request = XML::build($params); $http = new Http(); $response = $http->request(static::QUERYORDER_URL, Http::POST, $request); if (empty($response)) { throw new Exception('Get ORDER Failure:'); } $transaction = XML::parse($response); //返回签名数据校验 if (empty($transaction) || empty($transaction['sign'])) { return false; } $sign = $transaction['sign']; unset($transaction['sign']); $signGenerator = new SignGenerator($transaction); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->mchKey; }); if ($sign !== $signGenerator->getResult()) { return false; } // 返回结果判断 if (isset($transaction['result_code']) && $transaction['result_code'] === 'FAIL') { throw new Exception($transaction['err_code'] . ': ' . $transaction['err_code_des']); } if (isset($transaction['return_code']) && $transaction['return_code'] === 'FAIL') { throw new Exception($transaction['return_code'] . ': ' . $transaction['return_msg']); } return $transactionInfo = new Bag($transaction); }
/** * 查询红包信息 * * @param string $mchBillNumber * * @return array */ public function query($mchBillNumber) { if (empty($mchBillNumber)) { throw new Exception('mch_id is required'); } $param['mch_billno'] = $mchBillNumber; $param['nonce_str'] = uniqid('pre_'); $param['mch_id'] = $this->business->mch_id; $param['appid'] = $this->business->appid; $param['bill_type'] = 'MCHT'; $signGenerator = new SignGenerator($param); $me = $this; $signGenerator->onSortAfter(function (SignGenerator $that) use($me) { $that->key = $me->business->mch_key; }); $sign = $signGenerator->getResult(); $param['sign'] = $sign; $request = XML::build($param); //设置Http使用的证书 $options['sslcert_path'] = $this->business->getClientCert(); $options['sslkey_path'] = $this->business->getClientKey(); $http = new Http(); $response = $http->request(static::API_QUERY, Http::POST, $request, $options); if (empty($response)) { throw new Exception('Get LuckMoneyInfo failed.'); } $result = XML::parse($response); return $result; }
/** * 获取统一下单结果 * * @param bool|false $force 是否忽略缓存强制更新 * * @return array * @throws Exception * @throws \Overtrue\Wechat\Exception */ public function getResponse($force = false) { if (is_null($this->business)) { throw new Exception('Business is required'); } if (is_null($this->order)) { throw new Exception('Order is required'); } if ($this->unifiedOrder !== null && $force === false) { return $this->unifiedOrder; } $params = $this->order->toArray(); $params['appid'] = $this->business->appid; $params['mch_id'] = $this->business->mch_id; $signGenerator = new SignGenerator($params); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->business->mch_key; }); $params['sign'] = $signGenerator->getResult(); $request = XML::build($params); $http = new Http(); $response = $http->request(static::UNIFIEDORDER_URL, Http::POST, $request); if (empty($response)) { throw new Exception('Get UnifiedOrder Failure:'); } $unifiedOrder = XML::parse($response); if (isset($unifiedOrder['result_code']) && $unifiedOrder['result_code'] === 'FAIL') { throw new Exception($unifiedOrder['err_code'] . ': ' . $unifiedOrder['err_code_des']); } if (isset($unifiedOrder['return_code']) && $unifiedOrder['return_code'] === 'FAIL') { throw new Exception($unifiedOrder['return_code'] . ': ' . $unifiedOrder['return_msg']); } return $this->unifiedOrder = $unifiedOrder; }
/** * 获取退款结果 * @return array * @throws Exception */ public function getResponse() { if (is_null($this->business)) { throw new Exception('Business is required'); } static::$params['appid'] = $this->business->appid; static::$params['mch_id'] = $this->business->mch_id; $this->checkParams(); $signGenerator = new SignGenerator(static::$params); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->business->mch_key; }); static::$params['sign'] = $signGenerator->getResult(); $request = XML::build(static::$params); //设置Http使用的证书 $options['sslcert_path'] = $this->business->getClientCert(); $options['sslkey_path'] = $this->business->getClientKey(); $http = new Http(); $response = $http->request(static::REFUNDORDER_URL, Http::POST, $request, $options); if (empty($response)) { throw new Exception('Get Refund Failure:'); } $refundOrder = XML::parse($response); if (isset($refundOrder['return_code']) && $refundOrder['return_code'] === 'FAIL') { throw new Exception($refundOrder['return_code'] . ': ' . $refundOrder['return_msg']); } //返回签名数据校验 if (empty($refundOrder) || empty($refundOrder['sign'])) { throw new Exception('param sign is missing or empty'); } $sign = $refundOrder['sign']; unset($refundOrder['sign']); $signGenerator = new SignGenerator($refundOrder); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->business->mch_key; }); if ($sign !== $signGenerator->getResult()) { throw new Exception('check sign error'); } //返回结果判断 if (isset($refundOrder['result_code']) && $refundOrder['result_code'] === 'FAIL') { throw new Exception($refundOrder['err_code'] . ': ' . $refundOrder['err_code_des']); } if (isset($refundOrder['return_code']) && $refundOrder['return_code'] === 'FAIL') { throw new Exception($refundOrder['return_code'] . ': ' . $refundOrder['return_msg']); } return $this->refundInfo = $refundOrder; }
/** * 生成用于摇红包JSAPI调用的数据 * * @param string $lotteryId * @param string $openId * @param string $key * * @return string */ public function getJsPackage($lotteryId, $openId, $key) { $param['noncestr'] = uniqid('pre_'); $param['lottery_id'] = $lotteryId; $param['openid'] = $openId; $signGenerator = new SignGenerator($param); $signGenerator->onSortAfter(function (SignGenerator $that) use($key) { $that->key = $key; }); $sign = $signGenerator->getResult(); $param['sign'] = $sign; return json_encode($param); }