/** * 构造方法 */ 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); }
/** * 构造方法 */ public function __construct(Wechat $wechat, array $optionsOfUnifiedorder) { parent::__construct($wechat, $optionsOfUnifiedorder); $response = $this->getResponse(); $options = array('appid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'prepay_id' => $response['prepay_id'], 'nonce_str' => uniqid(), 'return_code' => self::SUCCESS, 'result_code' => self::SUCCESS); ksort($options); $signature = http_build_query($options); $signature = urldecode($signature); $signature = strtoupper(md5($signature . '&key=' . $wechat['mchkey'])); $options['sign'] = $signature; $this->options = $options; }
// print_r($business->getParams()); /** * 第 2 步:定义订单 */ $order = new Order(); $order->body('iphone 6 plus'); $order->out_trade_no(date('Y-m-dHis') . mt_rand(10000, 99999)); $order->total_fee('1'); // $order->openid($_SESSION['openid']); $order->openid('oWY-5jjLjo7pYUK86JPpwvcnF2Js'); $order->notify_url('http://code.1999.me/Wechat/example/payment_notify.php'); // print_r($order->getParams()); /** * 第 3 步:统一下单 */ $unifiedorder = new Unifiedorder(); $unifiedorder->setBusiness($business); $unifiedorder->setOrder($order); /** * 第 4 步:生成支付配置文件 */ $o = new Payment($unifiedorder); $configJSON = $o->getConfig(); ?> <!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>
<?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);
/** * 构造方法 */ public function __construct(Unifiedorder $unifiedorder) { $unifiedorder->set('trade_type', 'NATIVE'); $this->unifiedorder = $unifiedorder; }
/** * 构造方法 */ public function __construct(Wechat $wechat, array $optionsOfUnifiedorder) { parent::__construct($wechat, $optionsOfUnifiedorder); }
/** * 微信支付 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()); }
* 配置订单信息(这一步在实际应用中就是根据 $request 中的 product_id 在应用中获取订单信息) */ $requestBag = new Bag(); $requestBag->set('appid', $request['appid']); $requestBag->set('mch_id', $request['mch_id']); $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'); // 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');
/** * 构造方法 */ public function __construct(Wechat $wechat, array $optionsForUnifiedorder) { // 将 trade_type 强制设为 JSAPI $optionsForUnifiedorder['trade_type'] = self::TRADE_TYPE; parent::__construct($wechat, $optionsForUnifiedorder); }