Skip to content

Commit dd784bf

Browse files
committed
增加模式一的二维码支付
1 parent 6b5cac5 commit dd784bf

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

Wechat/Lib/Tools.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,27 @@ static public function getPaySign($option, $partnerKey) {
7070
* @return string
7171
*/
7272
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id') {
73+
return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
74+
}
7375

74-
function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
75-
foreach ($data as $key => $val) {
76-
is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
77-
$content .= "<{$key}>";
78-
if (is_array($val) || is_object($val)) {
79-
$content .= _data_to_xml($val);
80-
} elseif (is_numeric($val)) {
81-
$content .= $val;
82-
} else {
83-
$content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
84-
}
85-
list($_key,) = explode(' ', $key . ' ');
86-
$content .= "</$_key>";
76+
static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
77+
foreach ($data as $key => $val) {
78+
is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
79+
$content .= "<{$key}>";
80+
if (is_array($val) || is_object($val)) {
81+
$content .= self::_data_to_xml($val);
82+
} elseif (is_numeric($val)) {
83+
$content .= $val;
84+
} else {
85+
$content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
8786
}
88-
return $content;
87+
list($_key,) = explode(' ', $key . ' ');
88+
$content .= "</$_key>";
8989
}
90-
91-
return "<{$root}>" . _data_to_xml($data, $item, $id) . "</{$root}>";
90+
return $content;
9291
}
9392

93+
9494
/**
9595
* 将xml转为array
9696
* @param string $xml

Wechat/WechatPay.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class WechatPay {
1818
public $appid;
1919

2020
/** 商户身份ID */
21-
protected $mch_id;
21+
public $mch_id;
2222

2323
/** 商户支付密钥Key */
24-
protected $partnerKey;
24+
public $partnerKey;
2525

2626
/** 证书路径 */
27-
protected $ssl_cer;
28-
protected $ssl_key;
27+
public $ssl_cer;
28+
public $ssl_key;
2929

3030
/** 执行错误消息及代码 */
3131
public $errMsg;
@@ -177,6 +177,52 @@ public function getPrepayId($openid, $body, $out_trade_no, $total_fee, $notify_u
177177
return ($trade_type === 'JSAPI') ? $result['prepay_id'] : $result['code_url'];
178178
}
179179

180+
/**
181+
* 获取二维码预支付ID
182+
* @param string $openid 用户openid,JSAPI必填
183+
* @param string $body 商品标题
184+
* @param string $out_trade_no 第三方订单号
185+
* @param int $total_fee 订单总价
186+
* @param string $notify_url 支付成功回调地址
187+
* @param string $goods_tag 商品标记,代金券或立减优惠功能的参数
188+
* @return bool|string
189+
*/
190+
public function getQrcPrepayId($openid, $body, $out_trade_no, $total_fee, $notify_url, $goods_tag = null) {
191+
$postdata = array(
192+
"body" => $body,
193+
"out_trade_no" => $out_trade_no,
194+
"total_fee" => $total_fee,
195+
"notify_url" => $notify_url,
196+
"trade_type" => 'NATIVE',
197+
"spbill_create_ip" => Tools::getAddress()
198+
);
199+
empty($goods_tag) || $postdata['goods_tag'] = $goods_tag;
200+
empty($openid) || $postdata['openid'] = $openid;
201+
$result = $this->getArrayResult($postdata, self::MCH_BASE_URL . '/pay/unifiedorder');
202+
if (false === $this->_parseResult($result) || empty($result['prepay_id'])) {
203+
return false;
204+
}
205+
return $result['prepay_id'];
206+
}
207+
208+
/**
209+
* 获取支付规二维码
210+
* @param string $product_id 商户定义的商品id 或者订单号
211+
* @return string
212+
*/
213+
public function getQrcPayUrl($product_id) {
214+
$data = array(
215+
'appid' => $this->appid,
216+
'mch_id' => $this->mch_id,
217+
'time_stamp' => (string)time(),
218+
'nonce_str' => Tools::createNoncestr(),
219+
'product_id' => (string)$product_id,
220+
);
221+
$data['sign'] = Tools::getPaySign($data, $this->partnerKey);
222+
return "weixin://wxpay/bizpayurl?" . http_build_query($data);
223+
}
224+
225+
180226
/**
181227
* 创建JSAPI支付参数包
182228
* @param string $prepay_id

0 commit comments

Comments
 (0)