Completed
Push — master ( 9bc9b6...ec4bbc )
by Wei
04:31
created

Mchpay::rsaEncrypt()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 3
rs 10
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
	public function rsaEncrypt($data,$pubkey = null){
87
		if(!$pubkey) $pubkey = $this->getPublicKey();
88
		$encrypted = null;
89
		$pubkey = openssl_get_publickey($pubkey);
90
		if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING))
91
			$data = base64_encode($encrypted);
92 1
		else
93 1
			throw new Exception('Unable to encrypt data');
94 1
		return $data;
95 1
	}
96 1
97 1
	/**
98 1
	 * 查询企业付款银行卡
99
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_3
100
	 * @param $partner_trade_no
101
	 * @return array
102
	 */
103
	public function queryTransferBankCard($partner_trade_no){
104
		$data = array();
105
		$data["appid"] = $this->config["app_id"];
106
		$data["mch_id"] = $this->config["mch_id"];
107
		$data["partner_trade_no"] = $partner_trade_no;
108 3
		$result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true);
109 3
		return $result;
110 2
	}
111 1
112
	/**
113 2
	 * 获取RSA加密公钥
114 2
	 * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7&index=4
115 2
	 * @param bool $refresh
116 2
	 * @return string
117 1
	 * @throws Exception
118 1
	 */
119 1
	public function getPublicKey($refresh = false){
120 1
		if(!$this->publicKey) {
121 1
			if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) {
122
				$this->publicKey = file_get_contents($this->config["rsa_pubkey_path"]);
123
			}else{
124
				$data = array();
125 2
				$data["mch_id"] = $this->config["mch_id"];
126
				$data["sign_type"] = $this->config["sign_type"];
127
				$result = $this->post(self::URL_GETPUBLICKEY, $data, true);
128 2
				$pubkey = $result['pub_key'];
129 2
				$this->publicKey = $this->convertPKCS1toPKCS8($pubkey);
130 2
				if($fp = @fopen($this->config["rsa_pubkey_path"], "w")) {
131
					fwrite($fp, $this->publicKey);
132 1
					fclose($fp);
133 1
				}
134 1
			}
135 1
		}
136 1
		return $this->publicKey;
137 1
	}
138 1
139
	public function setPublicKey($publicKey){
140
		$this->publicKey = $publicKey;
141
	}
142
143
	private function convertPKCS1toPKCS8($pkcs1){
144
		$start_key = $pkcs1;
145
		$start_key = str_replace('-----BEGIN RSA PUBLIC KEY-----', '', $start_key);
146
		$start_key = trim(str_replace('-----END RSA PUBLIC KEY-----', '', $start_key));
147
		$key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' . str_replace("\n", '', $start_key);
148
		$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
149
		return $key;
150
	}
151
}