/** * 获取二维码支付地址(模式 1) */ public static function getForeverPayurl(Bag $bag, $key) { if (!$bag->has('time_stamp')) { $bag->set('time_stamp', time()); } if (!$bag->has('nonce_str')) { $bag->set('nonce_str', Util::randomString()); } $requireds = array('appid', 'mch_id', 'time_stamp', 'nonce_str', 'product_id'); foreach ($requireds as $property) { if (!$bag->has($property)) { exit(sprintf('"%s" is required', $property)); } } foreach ($bag as $property => $value) { if (!in_array($property, $requireds)) { exit(sprintf('Invalid argument "%s"', $property)); } } $signGenerator = new SignGenerator($bag); $signGenerator->onSortAfter(function ($bag) use($key) { $bag->set('key', $key); }); $bag->set('sign', $signGenerator->getResult()); $bag->remove('key'); $query = http_build_query($bag->all()); return 'weixin://wxpay/bizpayurl?' . urlencode($query); }
/** * 构造方法 */ public function __construct(Wechat $wechat) { $config = array('appId' => $wechat->getAppid(), 'nonceStr' => Util::randomString(), 'timestamp' => time()); $params = array('jsapi_ticket' => $wechat->getTicket(), 'noncestr' => $config['nonceStr'], 'timestamp' => $config['timestamp'], 'url' => Util::currentUrl()); $query = urldecode(http_build_query($params)); $config['signature'] = sha1($query); $this->config = $config; }
/** * 构造方法 */ public function __construct(Wechat $wechat) { $config = array('appId' => $wechat->getAppid(), 'nonceStr' => Util::randomString(), 'timestamp' => time()); $signGenerator = new SignGenerator(array('jsapi_ticket' => $wechat->getTicket(), 'noncestr' => $config['nonceStr'], 'timestamp' => $config['timestamp'], 'url' => Util::currentUrl())); $signGenerator->setHashType('sha1'); $signGenerator->setUpper(false); $config['signature'] = $signGenerator->getResult(); $this->config = $config; }
<?php /** * 此示例为 微信支付(扫码支付 模式一) * https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1 */ require './config.php'; use Endroid\QrCode\QrCode; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Util\SignGenerator; /** * 创建订单 */ $params = array('appid' => APPID, 'mch_id' => MCHID, 'time_stamp' => time(), 'nonce_str' => Util::randomString(), 'product_id' => date('YmdHis') . mt_rand(10000, 99999)); $signGenerator = new SignGenerator($params); $signGenerator->onSortAfter(function ($that) { $that->addParams('key', MCHKEY); }); $params['sign'] = $signGenerator->getResult(); $links = 'weixin://wxpay/bizpayurl?' . http_build_query($params); /** * 生成支付二维码 * See https://github.com/endroid/QrCode */ $qrCode = new QrCode(); $qrCode->setText($links)->setSize(200)->setPadding(10)->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0)); ?> <!DOCTYPE html> <html lang="en"> <head>
*/ require './config.php'; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\OAuth; use Thenbsp\Wechat\Payment; use Thenbsp\Wechat\Payment\Order; use Thenbsp\Wechat\Payment\Business; use Thenbsp\Wechat\Payment\Unifiedorder; use Thenbsp\Wechat\Exception\PaymentException; /** * 以 JSAPI 方式的付款需要获取用户 Openid */ if (!isset($_SESSION['openid'])) { $o = new OAuth(APPID, APPSECRET); if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl()); } else { $token = $o->getAccessToken($_GET['code']); $_SESSION['openid'] = $token->openid; } } /** * 第 1 步:定义商户 * 设置商户证书路径请使用 setClientCert/setClientKey * $business->setClientCert('/your/path/to/apiclient_cert.pem'); * $business->setClientKey('/your/path/to/apiclient_key.pem'); */ $business = new Business(); $business->appid(APPID); $business->appsecret(APPSECRET); $business->mch_id(MCHID);
require './config.php'; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Util\Json; use Thenbsp\Wechat\Util\Cache; try { $cache = new Cache('../Storage'); } catch (\Exception $e) { exit($e->getMessage()); } if (!($request = @file_get_contents('php://input'))) { exit('Invalid Request'); } /** * 微信服务器请求过来的数据 */ $request = Util::XML2Array($request); $cache->set('payment_notify', $request); // 微信服务器会在支付成功后该求该 URL,以 XML 的方式提交此次支付信息,具体信息类似: // { // "appid":"wx345f3830c28971f4", // "bank_type":"CFT", // "cash_fee":"1", // "fee_type":"CNY", // "is_subscribe":"Y", // "mch_id":"1241642202", // "nonce_str":"fTbkMTXgMmnvTaO1", // "openid":"oWY-5jjLjo7pYUK86JPpwvcnF2Js", // "out_trade_no":"124164220220150803161536", // "result_code":"SUCCESS", // "return_code":"SUCCESS", // "sign":"4B8948BD3E831394C956B5DFA8AAEB1E",
$business->appsecret(APPSECRET); $business->mch_id(MCHID); $business->mch_key(MCHKEY); $order = new Order(); $order->body('iphone 6 plus'); $order->out_trade_no(date('Y-m-dHis') . mt_rand(10000, 99999)); $order->total_fee('1'); $order->openid($request['openid']); $order->notify_url('Your notify url'); $unifiedorder = new Unifiedorder($business, $order); $response = $unifiedorder->getResponse(); /** * 响应订单 */ $params = array('return_code' => 'SUCCESS', 'result_code' => 'SUCCESS', 'return_msg' => 'return message', 'appid' => $request['appid'], 'err_code_des' => 'err code description', 'mch_id' => $request['mch_id'], 'nonce_str' => $request['nonce_str'], 'prepay_id' => $response['prepay_id']); $signGenerator = new SignGenerator($params); $signGenerator->onSortAfter(function ($that) { $that->addParams('key', MCHKEY); }); $params['sign'] = $signGenerator->getResult(); $xml = Util::array2XML($params); echo $xml; // { // "appid":"wx345f3830c28971f4", // "openid":"oWY-5jjLjo7pYUK86JPpwvcnF2Js", // "mch_id":"1241642202", // "is_subscribe":"Y", // "nonce_str":"fhKb6Fkzm3UJrX95", // "product_id":"2015080618052364076", // "sign":"1ED0F1A052F5212009E7C5DB89C57789" // }
/** * 获取统一下单结果 */ public function getResponse() { $signGenerator = new SignGenerator($this->bag); $signGenerator->onSortAfter(function ($bag) { $bag->set('key', $this->key); }); // 生成签名 $sign = $signGenerator->getResult(); // 生成签名后移除 Key $this->bag->remove('key'); // 调置签名 $this->bag->set('sign', $sign); $body = Util::array2XML($this->bag->all()); $response = Request::post(static::UNIFIEDORDER_URL, $body, false); $response = Util::XML2Array($response); if (isset($response['result_code']) && $response['result_code'] === 'FAIL') { throw new PaymentException($response['err_code'] . ': ' . $response['err_code_des']); } if (isset($response['return_code']) && $response['return_code'] === 'FAIL') { throw new PaymentException($response['return_code'] . ': ' . $response['return_msg']); } return $response; }
/** * 发送响应 */ public function send() { $response = $this->bag->all(); $response = Util::array2XML($response); echo $response; }
<?php require './config.php'; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Util\Json; use Thenbsp\Wechat\Util\Cache; try { $cache = new Cache('../Storage'); } catch (\Exception $e) { exit($e->getMessage()); } if ($request = @file_get_contents('php://input')) { $content = Util::XML2Array($request); $cache->set('payment_notify', Json::encode($content)); } // 微信服务器会在支付成功后该求该 URL,以 XML 的方式提交此次支付信息,具体信息类似: // { // "appid":"wx345f3830c28971f4", // "bank_type":"CFT", // "cash_fee":"1", // "fee_type":"CNY", // "is_subscribe":"Y", // "mch_id":"1241642202", // "nonce_str":"fTbkMTXgMmnvTaO1", // "openid":"oWY-5jjLjo7pYUK86JPpwvcnF2Js", // "out_trade_no":"124164220220150803161536", // "result_code":"SUCCESS", // "return_code":"SUCCESS", // "sign":"4B8948BD3E831394C956B5DFA8AAEB1E", // "time_end":"20150803161547", // "total_fee":"1",
$o = new OAuth(APPID, APPSECRET); /** * 授权流程 */ if (!isAuthorize()) { // 跳转至授权页 if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl(), 'snsapi_userinfo'); } else { try { $token = $o->getToken($_GET['code']); } catch (OAuthException $e) { exit($e->getMessage()); } setAuthorize($token); header('Location: ' . Util::currentUrl()); } } /** * 根据 Token 来获取用户信息 */ $token = getAuthorize(); $user = $o->getUser($token); echo '<pre>'; var_dump($token); var_dump($user); echo '</pre>'; /** * 刷新 TOKEN */ if (!$o->accessTokenIsValid($token)) {
$business = new Business(); $business->appid(APPID); $business->appsecret(APPSECRET); $business->mch_id(MCHID); $business->mch_key(MCHKEY); // print_r($business->getParams()); /** * 第 2 步:定义订单 */ $order = new Order(); $order->body('iphone 6 plus'); $order->out_trade_no(date('Y-m-dHis') . mt_rand(10000, 99999)); $order->total_fee('1'); // $order->openid($_SESSION['openid']); $order->openid('oWY-5jjLjo7pYUK86JPpwvcnF2Js'); $order->notify_url(Util::currentUrl()); // print_r($order->getParams()); /** * 第 3 步:统一下单 */ $unifiedorder = new Unifiedorder(); $unifiedorder->setBusiness($business); $unifiedorder->setOrder($order); /** * 第 4 步:生成支付配置文件 */ $o = new Payment($unifiedorder); $configJSON = $o->getConfigJssdk(); ?> <!DOCTYPE html>
$requestBag->set('trade_type', 'NATIVE'); // NATIVE 时不需要 Openid /** * 统一下单 */ $unifiedorder = new Unifiedorder($requestBag, MCHKEY); try { $response = $unifiedorder->getResponse(); } catch (PaymentException $e) { exit($e->getMessage()); } /** * 响应订单参数包 */ $responseBag = new Bag(); $responseBag->set('appid', $request['appid']); $responseBag->set('mch_id', $request['mch_id']); $responseBag->set('nonce_str', $request['nonce_str']); $responseBag->set('prepay_id', $response['prepay_id']); $responseBag->set('return_code', 'SUCCESS'); $responseBag->set('result_code', 'SUCCESS'); $responseBag->set('return_msg', 'return message'); $responseBag->set('err_code_des', 'err code description'); $signGenerator = new SignGenerator($responseBag); $signGenerator->onSortAfter(function ($bag) use($unifiedorder) { $bag->set('key', $unifiedorder->getKey()); }); $responseBag->set('sign', $signGenerator->getResult()); $responseBag->remove('key'); $xml = Util::array2XML($responseBag->all()); echo $xml;
/** * 获取统一下单结果 */ public function getResponse() { $params = $this->order->getParams(); $params['appid'] = $this->business->getParams('appid'); $params['mch_id'] = $this->business->getParams('mch_id'); ksort($params); $sign = http_build_query($params); $sign = urldecode($sign) . '&key=' . $this->business->getParams('mch_key'); $sign = strtoupper(md5($sign)); $params['sign'] = $sign; $request = Util::array2XML($params); $response = $this->curl->post(static::UNIFIEDORDER_URL, $request); if (empty($response)) { throw new PaymentException(sprintf('Get Unifiedorder Failure: %s', $this->curl->error())); } $object = Util::XML2Array($response); if (isset($object['result_code']) && $object['result_code'] === 'FAIL') { throw new PaymentException($object['err_code'] . ': ' . $object['err_code_des']); } if (isset($object['return_code']) && $object['return_code'] === 'FAIL') { throw new PaymentException($object['return_code'] . ': ' . $object['return_msg']); } return $object; }
<?php require './config.php'; use Thenbsp\Wechat\User; use Thenbsp\Wechat\OAuth; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Exception\OAuthException; // $openid = 'oWY-5jjLjo7pYUK86JPpwvcnF2Js'; /** * 网页授权获取用户信息 */ $o = new OAuth(APPID, APPSECRET); if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl(), 'snsapi_userinfo'); } else { /** * 根据 code 换取 accessToken */ try { $token = $o->getAccessToken($_GET['code']); // echo '<pre>'; // var_dump($token); // echo '</pre>'; } catch (OAuthException $e) { exit($e->getMessage()); } /** * 根据 accessToken 获取用户信息 */ try { $user = new User($token->access_token, $token->openid);