|
1
|
|
|
<?php |
|
2
|
|
|
namespace zhangv\wechat\pay\service; |
|
3
|
|
|
use \zhangv\wechat\pay\WechatPay; |
|
4
|
|
|
use \Exception; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* 公众号支付 |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
* @zhangv |
|
10
|
|
|
* @link https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 |
|
11
|
|
|
* |
|
12
|
|
|
* @method mixed queryOrderByOutTradeNo($out_trade_no) |
|
13
|
|
|
* @method mixed queryOrderByTransactionId($transaction_id) |
|
14
|
|
|
* @method mixed closeOrder($out_trade_no) |
|
15
|
|
|
* @method mixed refundByOutTradeNo($out_trade_no, $out_refund_no, $total_fee, $refund_fee, $ext = array()) |
|
16
|
|
|
* @method mixed refundByTransactionId($transaction_id, $out_refund_no, $total_fee, $refund_fee, $ext = array()) |
|
17
|
|
|
* @method mixed queryRefundByOutRefundNo($out_refund_no, $offset = 0) |
|
18
|
|
|
* @method mixed queryRefundByOutTradeNo($out_trade_no, $offset = 0) |
|
19
|
|
|
* @method mixed queryRefundByRefundId($refund_id, $offset = 0) |
|
20
|
|
|
* @method mixed queryRefundByTransactionId($transaction_id, $offset = 0) |
|
21
|
|
|
* @method mixed downloadBill($bill_date, $bill_type = 'ALL') |
|
22
|
|
|
* @method mixed downloadFundFlow($bill_date, $account_type = WechatPay::ACCOUNTTYPE_BASIC, $tar_type = 'GZIP') |
|
23
|
|
|
* @method mixed onPaidNotify($notify_data, callable $callback = null) |
|
24
|
|
|
* @method mixed onRefundedNotify($notify_data, callable $callback = null) |
|
25
|
|
|
* @method mixed report($interface_url, $execution_time, $return_code, $result_code, $user_ip, $out_trade_no = null, $time = null, $device_info = null,$return_msg = null,$err_code = null,$err_code_des = null) |
|
26
|
|
|
* @method mixed batchQueryComment($begin_time, $end_time, $offset = 0, $limit = 200) |
|
27
|
|
|
*/ |
|
28
|
|
|
class Jsapi extends WechatPay { |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* 获取预支付单信息(prepay_id) |
|
32
|
|
|
* @param $body string 内容 |
|
33
|
|
|
* @param $out_trade_no string 商户订单号 |
|
34
|
|
|
* @param $total_fee int 总金额 |
|
35
|
|
|
* @param $openid string openid |
|
36
|
|
|
* @param $spbill_create_ip |
|
37
|
|
|
* @param $ext array |
|
38
|
|
|
* @return string |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function getPrepayId($body,$out_trade_no,$total_fee,$openid,$spbill_create_ip = null,$ext = null) { |
|
42
|
2 |
|
$data = ($ext && is_array($ext))?$ext:array(); |
|
43
|
2 |
|
$data["body"] = $body; |
|
44
|
2 |
|
$data["out_trade_no"] = $out_trade_no; |
|
45
|
2 |
|
$data["total_fee"] = $total_fee; |
|
46
|
2 |
|
$data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
|
47
|
2 |
|
$data["notify_url"] = $this->config["notify_url"]; |
|
48
|
2 |
|
$data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
|
49
|
2 |
|
if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
50
|
2 |
|
$data["openid"] = $openid; |
|
51
|
2 |
|
$result = $this->unifiedOrder($data); |
|
52
|
2 |
|
return $result["prepay_id"]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* 获取支付参数 |
|
57
|
|
|
* @param $prepay_id string 预支付ID |
|
58
|
|
|
* @return array |
|
59
|
|
|
* @throws Exception |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getPackage($prepay_id) { |
|
62
|
2 |
|
$data = array(); |
|
63
|
2 |
|
$data["package"] = "prepay_id=$prepay_id"; |
|
64
|
2 |
|
$data["timeStamp"] = time(); |
|
65
|
2 |
|
$data["nonceStr"] = $this->getNonceStr(); |
|
66
|
2 |
|
$data["appId"] = $this->config["app_id"]; |
|
67
|
2 |
|
$data["signType"] = "MD5"; |
|
68
|
2 |
|
$data["paySign"] = $this->sign($data); |
|
69
|
2 |
|
return $data; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |