/** * 构造方法 */ public function __construct(Wechat $wechat, array $optionsForUnifiedorder) { // 将 trade_type 强制设为 JSAPI $optionsForUnifiedorder['trade_type'] = self::TRADE_TYPE; // 去统一下单接口下单 $unifiedorder = new Unifiedorder($wechat, $optionsForUnifiedorder); // 下单结果中的 prepay_id $response = $unifiedorder->getResponse(); $this->wechat = $wechat; parent::__construct(array('package' => sprintf('prepay_id=%s', $response['prepay_id']))); }
/** * 成功响应 */ 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); }
<?php require './_example.php'; use Thenbsp\Wechat\Payment\Unifiedorder; $options = array('body' => 'iphone 6 plus', 'total_fee' => 1, 'openid' => 'oWY-5jjLjo7pYUK86JPpwvcnF2Js', 'out_trade_no' => date('YmdHis') . mt_rand(10000, 99999), 'notify_url' => 'http://----------YOUR NOTIFY URL----------/_example/payment-notify.php'); $unifiedorder = new Unifiedorder($wechat, $options); try { $response = $unifiedorder->getResponse(); } catch (Exception $e) { exit($e->getMessage()); } print_r($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()); }