/** * 构造方法 */ 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(Bag $bag, $key) { // 检测必填字段 foreach ($this->required as $paramName) { if (!$bag->has($paramName)) { throw new PaymentException(sprintf('"%s" is required', $paramName)); } } // 生成签名 $signGenerator = new SignGenerator($bag); $signGenerator->onSortAfter(function ($that) use($key) { $that->set('key', $key); }); $bag->set('sign', $signGenerator->getResult()); $bag->remove('key'); $this->bag = $bag; }