/** * 构造方法 */ public function __construct(Bag $bag, $key) { if (!$bag->has('trade_type')) { $bag->set('trade_type', 'JSAPI'); } if (!$bag->has('nonce_str')) { $bag->set('nonce_str', Util::randomString()); } if (!$bag->has('spbill_create_ip')) { $bag->set('spbill_create_ip', Util::getClientIp()); } // 检测必填字段 foreach ($this->required as $paramName) { if (!$bag->has($paramName)) { throw new PaymentException(sprintf('"%s" is required', $paramName)); } } // trade_type 为 JSAPI 时,必需包含 Openid if ($bag->get('trade_type') === 'JSAPI') { if (!$bag->has($paramName)) { throw new PaymentException('"Openid" is required'); } } $this->bag = $bag; $this->key = $key; }
/** * 获取二维码支付地址(模式 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; }
/** * 设置订单 */ public function setOrder(Order $order) { if (null !== $order) { try { $order->checkParams(); } catch (Exception $e) { throw new PaymentException($e->getMessage()); } if (!$order->hasParams('nonce_str')) { $order->nonce_str(Util::randomString()); } if (!$order->hasParams('spbill_create_ip')) { $order->spbill_create_ip(Util::clientIP()); } if (!$order->hasParams('trade_type')) { if (!$order->hasParams('openid')) { throw new PaymentException('openid is required'); } $order->trade_type('JSAPI'); } $this->order = $order; } }
<?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>