/** * 构造方法 */ 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; }
/** * 生成配置 */ private function _generateConfig($asJson = true) { $response = $this->unifiedorder->getResponse(); $business = $this->unifiedorder->getBusiness(); $config = array('appId' => $business->getParams('appid'), 'timeStamp' => time(), 'nonceStr' => $response['nonce_str'], 'package' => 'prepay_id=' . $response['prepay_id'], 'signType' => 'MD5'); $signGenerator = new SignGenerator($config); $signGenerator->onSortAfter(function ($that) use($business) { $that->addParams('key', $business->getParams('mch_key')); }); $config['paySign'] = $signGenerator->getResult(); return $config; }
/** * 构造方法 */ 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; }
<?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>
/** * 获取统一下单结果 */ 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; }
/** * 微信支付 WeixinJSBridge invoke 方式配置文件 */ public static function getPaymentConfig(Unifiedorder $unifiedorder, $asArray = false) { $bag = $unifiedorder->getBag(); $res = $unifiedorder->getResponse(); $config = new Bag(); $config->set('appId', $bag->get('appid')); $config->set('timeStamp', time()); $config->set('nonceStr', $res['nonce_str']); $config->set('package', 'prepay_id=' . $res['prepay_id']); $config->set('signType', 'MD5'); $signGenerator = new SignGenerator($config); $signGenerator->onSortAfter(function ($that) use($unifiedorder) { $that->set('key', $unifiedorder->getKey()); }); $config->set('paySign', $signGenerator->getResult()); $config->remove('key'); return $asArray ? $config->all() : JSON::encode($config->all()); }
$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;