File size: 7,038 Bytes
0d5b03e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php

class umfpay_plugin

{
	static public $info = [
		'name'        => 'umfpay', //支付插件英文名称,需和目录名称一致,不能有重复
		'showname'    => '联动优势', //支付插件显示名称
		'author'      => '联动优势', //支付插件作者
		'link'        => 'https://xy.umfintech.com/', //支付插件作者链接
		'types'       => ['alipay','wxpay','bank'], //支付插件支持的支付方式,可选的有alipay,qqpay,wxpay,bank
		'inputs' => [ //支付插件要求传入的参数以及参数显示名称,可选的有appid,appkey,appsecret,appurl,appmchid
			'appid' => [
				'name' => '商户编号',
				'type' => 'input',
				'note' => '',
			],
			'appkey' => [
				'name' => '商户密钥',
				'type' => 'input',
				'note' => '此项随便填写',
			],
		],
		'select' => null,
		'note' => '将平台公钥cert.pem和商户私钥key.pem放到/plugins/umfpay/cert/文件夹下', //支付密钥填写说明
		'bindwxmp' => false, //是否支持绑定微信公众号
		'bindwxa' => false, //是否支持绑定微信小程序
	];

	static public function submit(){
		global $siteurl, $channel, $order, $sitename;

		if($order['typename']=='alipay'){
			return ['type'=>'jump','url'=>'/pay/alipay/'.TRADE_NO.'/'];
		}elseif($order['typename']=='wxpay'){
			if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!==false){
				return ['type'=>'jump','url'=>'/pay/wxjspay/'.TRADE_NO.'/?d=1'];
			}else{
				return ['type'=>'jump','url'=>'/pay/wxpay/'.TRADE_NO.'/'];
			}
		}elseif($order['typename']=='bank'){
			return ['type'=>'jump','url'=>'/pay/bank/'.TRADE_NO.'/'];
		}
	}

	static public function mapi(){
		global $siteurl, $channel, $order, $conf, $device, $mdevice;

		if($order['typename']=='alipay'){
			return self::alipay();
		}elseif($order['typename']=='wxpay'){
			if($mdevice=='wechat'){
				return self::wxjspay();
			}else{
				return self::wxpay();
			}
		}elseif($order['typename']=='bank'){
			return self::bank();
		}
	}

	//扫码支付
	static private function qrcode($type){
		global $siteurl, $channel, $order, $ordername, $conf, $clientip;

		require(PAY_ROOT."inc/UmfService.class.php");

		$params = [
			'service' => 'active_scancode_order_new',
			'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
			'goods_inf' => $ordername,
			'order_id' => TRADE_NO,
			'mer_date' => date("Ymd"),
			'amount' => strval($order['realmoney']*100),
			'user_ip' => $clientip,
			'scancode_type' => $type,
			'mer_flag' => 'KMER',
			'consumer_id' => str_replace('.','',$clientip)
		];

		$client = new UmfService($channel['appid']);
		$result = $client->submit($params);
		
        if (isset($result['ret_code']) && $result['ret_code'] == '0000') {
			return base64_decode($result['bank_payurl']);
        } elseif(isset($result['ret_code'])) {
			throw new Exception('['.$result['ret_code'].']'.$result['ret_msg']);
		}else{
			throw new Exception('返回数据解析失败');
		}
	}

	//支付宝扫码支付
	static public function alipay(){
		try{
			$code_url = self::qrcode('ALIPAY');
		}catch(Exception $ex){
			return ['type'=>'error','msg'=>'支付宝支付下单失败!'.$ex->getMessage()];
		}

		return ['type'=>'qrcode','page'=>'alipay_qrcode','url'=>$code_url];
	}

	//微信扫码支付
	static public function wxpay(){
		try{
			$code_url = self::qrcode('WECHAT');
		}catch(Exception $ex){
			return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
		}

		if (checkmobile()==true) {
			return ['type'=>'qrcode','page'=>'wxpay_wap','url'=>$code_url];
		} else {
			return ['type'=>'qrcode','page'=>'wxpay_qrcode','url'=>$code_url];
		}
	}

	//微信公众号支付
	static public function wxjspay(){
		global $siteurl, $channel, $order, $ordername, $conf, $clientip;

		require(PAY_ROOT."inc/UmfService.class.php");

		$params = [
			'service' => 'publicnumber_and_verticalcode',
			'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
			'ret_url' => $siteurl . 'pay/return/' . TRADE_NO . '/',
			'goods_inf' => $ordername,
			'order_id' => TRADE_NO,
			'mer_date' => date("Ymd"),
			'amount' => strval($order['realmoney']*100),
			'user_ip' => $clientip,
			'is_public_number' => 'Y',
		];

		$client = new UmfService($channel['appid']);
		$url = $client->getpayurl($params);
		
        return ['type'=>'jump','url'=>$url];
	}

	//云闪付扫码支付
	static public function bank(){
		try{
			$code_url = self::qrcode('UNION');
		}catch(Exception $ex){
			return ['type'=>'error','msg'=>'云闪付下单失败!'.$ex->getMessage()];
		}

		return ['type'=>'qrcode','page'=>'bank_qrcode','url'=>$code_url];
	}

	//异步回调
	static public function notify(){
		global $channel, $order;

		require(PAY_ROOT."inc/UmfService.class.php");
		
		$client = new UmfService($channel['appid']);
		$verify_result = $client->verifySign($_GET);

		$params = [
			'order_id' => $_GET['order_id'],
			'mer_date' => $_GET['mer_date'],
		];

		if($verify_result) {//验证成功

			if ($_GET['trade_state'] == 'TRADE_SUCCESS') {
				if($_GET['order_id'] == TRADE_NO){
					processNotify($order, $_GET['trade_no'], $_GET['mer_cust_id']);
				}
			}else{
				$params['ret_code'] = '0001';
				$params['ret_msg'] = 'trade_state'.$_GET['trade_state'];
			}
		}
		else {
			//验证失败
			$params['ret_code'] = '0001';
			$params['ret_msg'] = 'sign fail';
		}

		$response_str = $client->responseUMFstr($params);

		$html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
		$html .= '<html><head>';
		$html .= '<META NAME="MobilePayPlatform" CONTENT="'.$response_str.'">';
		$html .= '</head>';
		$html .= '<body></body>';
		$html .= '</html>';

		return ['type'=>'html','data'=>$html];
	}

	//同步回调
	static public function return(){
		return ['type'=>'page','page'=>'return'];
	}

	//退款
	static public function refund($order){
		global $channel;
		if(empty($order))exit();

		require(PAY_ROOT."inc/UmfService.class.php");

		$params = [
			'service' => 'mer_refund',
			'refund_no' => date("ymdHis").rand(1111,9999),
			'order_id' => $order['trade_no'],
			'mer_date' => substr($order['trade_no'], 0, 8),
			'org_amount' => strval($order['realmoney']*100),
			'refund_amount' => strval($order['refundmoney']*100),
		];
		
		try{
			$client = new UmfService($channel['appid']);
			$result = $client->submit($params);
		}catch(Exception $ex){
			return ['code'=>-1, 'msg'=>$ex->getMessage()];
		}

        if (isset($result['ret_code']) && $result['ret_code'] == '0000') {
			return ['code'=>0, 'trade_no'=>$result['order_id'], 'refund_fee'=>$result['refund_amt']];
        } elseif(isset($result['ret_code'])) {
			return ['code'=>-1, 'msg'=>'['.$result['ret_code'].']'.$result['ret_msg']];
		}else{
			return ['code'=>-1, 'msg'=>'未知错误'];
		}
	}
}