/** * 成功响应 */ public function success(Unifiedorder $unifiedorder) { $unifiedorder->set('trade_type', 'NATIVE'); $response = $unifiedorder->getResponse(); $options = array('appid' => $unifiedorder['appid'], 'mch_id' => $unifiedorder['mch_id'], 'prepay_id' => $response['prepay_id'], 'nonce_str' => Util::getRandomString(), 'return_code' => static::SUCCESS, 'result_code' => static::SUCCESS); // 按 ASCII 码排序 ksort($options); $signature = urldecode(http_build_query($options)); $signature = strtoupper(md5($signature . '&key=' . $unifiedorder->getKey())); $options['sign'] = $signature; $this->xmlResponse($options); }
/** * 构造方法 */ public function __construct(Unifiedorder $unifiedorder, array $defaults = array()) { $res = $unifiedorder->getResponse(); $key = $unifiedorder->getKey(); $config = array('appId' => $unifiedorder['appid'], 'timeStamp' => Util::getTimestamp(), 'nonceStr' => Util::getRandomString(), 'package' => 'prepay_id=' . $res['prepay_id'], 'signType' => 'MD5'); // 如果需要指定以上参数,可以通过 $defaults 变量传入 $options = array_replace($config, $defaults); ksort($options); $queryString = urldecode(http_build_query($options)); $paySign = strtoupper(md5($queryString . '&key=' . $key)); $options['paySign'] = $paySign; parent::__construct($options); }
/** * 微信支付 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;