Completed
Push — master ( 76d6fa...9bc9b6 )
by Wei
08:43
created

Mchpay::getPublicKey()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 1
dl 0
loc 18
ccs 14
cts 14
cp 1
crap 5
rs 9.4888
c 0
b 0
f 0
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/tools/mch_pay.php?chapter=14_1
11
 *
12
 */
13
class Mchpay extends WechatPay {
14
15
	/**
16
	 * 企业付款到零钱
17
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
18
	 * @param $partner_trade_no
19
	 * @param $openid
20
	 * @param $amount
21
	 * @param $desc
22
	 * @param $spbill_create_ip
23
	 * @param $check_name
24
	 * @param $re_user_name
25
	 * @return array
26
	 * @throws Exception
27
	 */
28 2
	public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){
29 2
		$data = array();
30 2
		if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required');
31 2
		$data["mch_appid"] = $this->config["app_id"];
32 2
		$data["mchid"] = $this->config["mch_id"];
33 2
		$data["partner_trade_no"] = $partner_trade_no;
34 2
		$data["openid"] = $openid;
35 2
		$data["amount"] = $amount;
36 2
		$data["desc"] = $desc;
37 2
		$data['spbill_create_ip'] = $spbill_create_ip?:$_SERVER['SERVER_ADDR'];
38 2
		$data["check_name"] = $check_name;
39 2
		$data["re_user_name"] = $re_user_name;
40 2
		$result = $this->post(self::URL_TRANSFER_WALLET,$data,true);
41 1
		return $result;
42
	}
43
44
	/**
45
	 * 查询企业付款
46
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3
47
	 * @param $partner_trade_no
48
	 * @return array
49
	 */
50 1
	public function queryTransferWallet($partner_trade_no){
51 1
		$data = array();
52 1
		$data["appid"] = $this->config["app_id"];
53 1
		$data["mch_id"] = $this->config["mch_id"];
54 1
		$data["partner_trade_no"] = $partner_trade_no;
55 1
		$result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true);
56 1
		return $result;
57
	}
58
59
	/**
60
	 * 企业付款到银行卡
61
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_2
62
	 * @param $partner_trade_no
63
	 * @param $bank_no
64
	 * @param $true_name
65
	 * @param $bank_code
66
	 * @param $amount
67
	 * @param $desc
68
	 * @return array
69
	 * @throws Exception
70
	 */
71 2
	public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){
72 2
		if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code");
73 2
		$data = array();
74 2
		$data["partner_trade_no"] = $partner_trade_no;
75 2
		$enc_bank_no = $this->rsaEncrypt($bank_no);
76 1
		$data["enc_bank_no"] = $enc_bank_no;
77 1
		$enc_true_name = $this->rsaEncrypt($true_name);
78 1
		$data["enc_true_name"] = $enc_true_name;
79 1
		$data["bank_code"] = $bank_code;
80 1
		$data["desc"] = $desc;
81 1
		$data["amount"] = $amount;
82 1
		$result = $this->post(self::URL_TRANSFER_BANKCARD,$data,true);
83 1
		return $result;
84
	}
85
86
	/**
87
	 * 查询企业付款银行卡
88
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_3
89
	 * @param $partner_trade_no
90
	 * @return array
91
	 */
92 1
	public function queryTransferBankCard($partner_trade_no){
93 1
		$data = array();
94 1
		$data["appid"] = $this->config["app_id"];
95 1
		$data["mch_id"] = $this->config["mch_id"];
96 1
		$data["partner_trade_no"] = $partner_trade_no;
97 1
		$result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true);
98 1
		return $result;
99
	}
100
101
	/**
102
	 * 获取RSA加密公钥
103
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7&index=4
104
	 * @param bool $refresh
105
	 * @return string
106
	 * @throws Exception
107
	 */
108 3
	public function getPublicKey($refresh = false){
109 3
		if(!$this->publicKey) {
110 2
			if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) {
111 1
				$this->publicKey = file_get_contents($this->config["rsa_pubkey_path"]);
112
			}else{
113 2
				$data = array();
114 2
				$data["mch_id"] = $this->config["mch_id"];
115 2
				$data["sign_type"] = $this->config["sign_type"];
116 2
				$result = $this->post(self::URL_GETPUBLICKEY, $data, true);
117 1
				$pubkey = $result['pub_key'];
118 1
				$this->publicKey = $this->convertPKCS1toPKCS8($pubkey);
119 1
				if($fp = @fopen($this->config["rsa_pubkey_path"], "w")) {
120 1
					fwrite($fp, $this->publicKey);
121 1
					fclose($fp);
122
				}
123
			}
124
		}
125 2
		return $this->publicKey;
126
	}
127
128 2
	public function setPublicKey($publicKey){
129 2
		$this->publicKey = $publicKey;
130 2
	}
131
132 1
	private function convertPKCS1toPKCS8($pkcs1){
133 1
		$start_key = $pkcs1;
134 1
		$start_key = str_replace('-----BEGIN RSA PUBLIC KEY-----', '', $start_key);
135 1
		$start_key = trim(str_replace('-----END RSA PUBLIC KEY-----', '', $start_key));
136 1
		$key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' . str_replace("\n", '', $start_key);
137 1
		$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
138 1
		return $key;
139
	}
140
}