/** * 构造方法 */ 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; }
/** * 构造方法 */ 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_5 */ require './config.php'; use Thenbsp\Wechat\Config; use Thenbsp\Wechat\Util\Bag; use Thenbsp\Wechat\Payment\Unifiedorder; /** * 配置订单信息 */ $bag = new Bag(); $bag->set('appid', APPID); $bag->set('mch_id', MCHID); $bag->set('notify_url', NOTIFY_URL); $bag->set('body', 'iphone 6 plus'); $bag->set('out_trade_no', date('YmdHis') . mt_rand(10000, 99999)); $bag->set('total_fee', 1); // 单位为 “分” $bag->set('trade_type', 'NATIVE'); // NATIVE 时不需要 Openid /** * 统一下单 */ $unifiedorder = new Unifiedorder($bag, MCHKEY); /** * 获取支付 URL(模式 2) */ $payurl = Config::getTemporaryPayurl($unifiedorder);
<?php /** * 此示例为 微信支付(扫码支付 模式一) * https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4 */ require './config.php'; use Thenbsp\Wechat\Config; use Thenbsp\Wechat\Util\Bag; /** * 配置订单参数 */ $bag = new Bag(); $bag->set('appid', APPID); $bag->set('mch_id', MCHID); $bag->set('product_id', date('YmdHis') . mt_rand(10000, 99999)); /** * 获取支付 URL(模式 1) */ $payurl = Config::getForeverPayurl($bag, MCHKEY); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>Wechat SDK</title> </head> <body ontouchstart="">
/** * 微信支付 JSSDK chooseWXPay 方式配置文件 */ public static function getPaymentJssdkConfig(Unifiedorder $unifiedorder, $asArray = false) { $config = static::getPaymentConfig($unifiedorder, true); $bag = new Bag(); $bag->set('timestamp', $config['timeStamp']); $bag->set('nonceStr', $config['nonceStr']); $bag->set('package', $config['package']); $bag->set('signType', $config['signType']); $bag->set('paySign', $config['paySign']); return $asArray ? $bag->all() : JSON::encode($bag->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());
use Thenbsp\Wechat\Config; use Thenbsp\Wechat\Util\Bag; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Payment\Unifiedorder; // 一、以 JSAPI 方式的付款需要获取用户 Openid if (!isset($_SESSION['openid'])) { $o = new OAuth(APPID, APPSECRET); if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl()); } else { $token = $o->getToken($_GET['code']); $_SESSION['openid'] = $token->openid; } } // 二、配置订单信息(参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) $bag = new Bag(); $bag->set('appid', APPID); $bag->set('mch_id', MCHID); $bag->set('notify_url', NOTIFY_URL); $bag->set('body', 'iphone 6 plus'); $bag->set('out_trade_no', date('YmdHis') . mt_rand(10000, 99999)); $bag->set('total_fee', 1); $bag->set('openid', $_SESSION['openid']); /** * 三、统一下单 */ $unifiedorder = new Unifiedorder($bag, MCHKEY); /** * 四、生成支付配置文件 */ $configJSON = Config::getPaymentConfig($unifiedorder, $asArray = false);
// 请求过来的 product_id,根据 product_id 去数据库里查询商品,提交到第二步: // $productId = $request->get('product_id'); /** * 二、统一下单 */ $requestBag = new Bag(); $requestBag->set('appid', APPID); $requestBag->set('mch_id', MCHID); $requestBag->set('notify_url', NOTIFY_URL); $requestBag->set('body', 'iphone 6 plus'); $requestBag->set('out_trade_no', date('YmdHis') . mt_rand(10000, 99999)); $requestBag->set('total_fee', 1); $requestBag->set('trade_type', 'NATIVE'); try { $unifiedorder = new Unifiedorder($requestBag, MCHKEY); $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'); $qrcodeResponse = new QrcodeCallbackResponse($responseBag, MCHKEY); $qrcodeResponse->send();