From be526e3f71cba3d00ba7ad4035d87a2f1a8da780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 21 May 2019 16:25:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Recharge.php=20=E6=94=B9?= =?UTF-8?q?=E9=94=99=E4=BA=86=EF=BC=8C=E6=81=A2=E5=A4=8D=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wx/Recharge.php | 249 ++++++++++++++++++++++-------------------------- 1 file changed, 115 insertions(+), 134 deletions(-) diff --git a/wx/Recharge.php b/wx/Recharge.php index 8a1c847..62d8235 100644 --- a/wx/Recharge.php +++ b/wx/Recharge.php @@ -1,153 +1,134 @@ $val) { - $this->$key = $val; - } + /** @var Recharge */ + private static $recharge; - $this->header = $this->reloadResponse($this->header); - } + private $money = 0; + + private $orderNo; + + private $data = []; + + /** + * @param int $money + * @param string $orderNo + * @return bool|Result + */ + public function payment(int $money, string $orderNo, $openId = NULL) + { + $_this = $this; + if ($money < 0) { + return new Result(['code' => 500, 'message' => '充值金额不能小于0.']); + } + $this->money = $money; + $this->orderNo = $orderNo; + $this->data['openid'] = $openId; + return Http::post($this->createPayUrl(), $this->builder(), + function ($result, $body) use ($_this) { + $data = $_this->toArray($result); + if (isset($data['sign'])) { + $sign = $data['sign']; + unset($data['sign']); + $_sign = $_this->sign($data); + } + $return = []; + if (!isset($sign) || $sign != $_sign) { + $return['code'] = -1; + $return['message'] = $data['return_msg'] ?? '返回数据签名验证失败'; + } else { + if ($data['return_code'] == 'FAIL') { + $return['code'] = -1; + $return['message'] = $data['return_msg']; + } else { + $return['code'] = 0; + $return['data'] = $data; + $return['data']['postBody'] = $body; + } + } + return $return; + }, ['Content-Type' => 'text/xml'] + ); + } - /** - * @param $header - * - * @return array - */ - private function reloadResponse($header) - { - $data = []; - $load = explode("\n", $header); - if (!empty($load) && is_array($load)) { - foreach ($load as $key => $val) { - if (empty($val)) continue; - $ex = explode(': ', $val); - if (!empty($ex[0]) && !empty($ex[1])) { - $data[trim($ex[0])] = trim($ex[1]); - } - } - } - return $data; - } + /** + * @return string + */ + protected function builder() + { + $data = [ + 'appid' => $this->appid, + 'mch_id' => $this->mch_id, + 'nonce_str' => $this->random(32), + 'body' => $this->body, + 'out_trade_no' => $this->orderNo, + 'total_fee' => $this->money, + 'sign_type' => $this->sign_type, + 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], + 'notify_url' => $this->notify_url, + 'trade_type' => $this->trade_type, + ]; - public function __get($name) - { - return $this->$name; - } + $data = array_merge($data, $this->data); + $data['sign'] = $this->sign($data); - public function __set($name, $value) - { - $this->$name = $value; + return static::toXml($data); + } - return $this; - } + private function createPayUrl() + { + return $this->mch_host . '/pay/unifiedorder'; + } - public function getTime() - { - return [ - 'startTime' => $this->startTime, - 'requestTime' => $this->requestTime, - 'runTime' => $this->runTime, - ]; - } + /** + * @param $money + * @param $openid + * @param $order + * @param $REMOTE_ADDR + * @return Result + * @throws + * + * 提现 + */ + public function tx($money, $openid, $order, $REMOTE_ADDR, $desc = NULL) + { + $array = [ + 'nonce_str' => $this->random(32), + 'partner_trade_no' => $order, + 'mchid' => $this->mch_id, + 'mch_appid' => $this->appid, + 'openid' => $openid, + 'check_name' => 'NO_CHECK', + 'amount' => $money * 100, + 'spbill_create_ip' => $REMOTE_ADDR, + 'desc' => $desc ?? '有大佬给你发红包啦.', + ]; - /** - * @param $key - * @param $data - * @return $this - * @throws \Exception - */ - public function setAttr($key, $data) - { - if (!property_exists($this, $key)) { - throw new \Exception('未查找到相应对象属性'); - } - $this->$key = $data; - return $this; - } + $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; + $array['sign'] = $this->sign($array); - public function isResultsOK() - { - return $this->code == 0; - } + return Http::post($transfers, static::toXml($array), function ($data) { + $array = $this->toArray($data); + if ($array['result_code'] != 'SUCCESS') { + $data = ['code' => $array['err_code'], 'message' => $array['err_code_des']]; + } else { + $data = ['code' => 0, 'message' => '支付成功']; + } + return new Result($data); + }, NULL, [$this->ssl_cert, $this->ssl_key]); + } - /** - * @param array $headers - * 批量设置返回头 - */ - public function setHeaders(array $headers) - { - foreach ($headers as $key => $val) { - $this->setHeader($key, $val); - } - } - - /** - * @param $key - * @param $val - * 设置返回头 - */ - public function setHeader($key, $val) - { - header($key . ':' . $val); - } - - /** - * @param string $name - * @return mixed - */ - public function getData($name = '') - { - if (!$this->isResultsOK()) { - return ''; - } - if (!empty($name) && isset($this->data[$name])) { - return $this->data[$name]; - } - return $this->data; - } - - /** - * @param $key - * @param $data - * @return $this - */ - public function append($key, $data) - { - $this->data[$key] = $data; - return $this; - } - - public function getMessage() - { - return $this->message; - } - - public function getCode() - { - return $this->code; - } }