支付工具包

官方文档:yansongda

原文地址

1
2
3
4
5
6
7
8
安装

composer require yansongda/pay -vvv

引入

use Yansongda\Pay\Pay;

微信支付:

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
//调起支付(仅示例扫码支付)
$wechat_config = Config::get('wechat_pay');
$config = [
'app_id' => $wechat_config['app_id'],
'mch_id' => $wechat_config['mch_id'],
'key' => $wechat_config['key'],
'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/portal/Order/wechat_notify',
'http' => [
'timeout' => 5.0,
'connect_timeout' => 5.0,
],
];
$pay_order = [
'out_trade_no' => $order['order_sn'],
'total_fee' => $order['price']*100, //微信支付以分为单位
'body' => '测试订单',
];
try {
//网站扫码支付
$pay = Pay::wechat($config)->scan($pay_order);
$qrcode = new Qrcode($pay->code_url);
$image = $qrcode->getString();
return response($image, 200, ['Content-Type' => 'image/jpeg']);
} catch (\Exception $e) {
echo $e->getMessage();
}

//回调验签(验签失败是会跑出异常的,最好写个捕获异常)
$pay = Pay::wechat($config);
$data = $pay->verify();
$param = $data->all();
//处理订单
if ($param['result_code']=='SUCCESS'&&$param['return_code']=='SUCCESS') {
(new OrderService($param['out_trade_no'], 'wechat'))->call_notice();
}

//退款(退款需要用到安全证书)
$wechat_config = Config::get('wechat_pay');
$config = [
'app_id' => $wechat_config['app_id'],
'mch_id' => $wechat_config['mch_id'],
'key' => $wechat_config['key'],
'cert_client' => $wechat_config['cert_client'],
'cert_key' => $wechat_config['cert_key'],
'http' => [
'timeout' => 5.0,
'connect_timeout' => 5.0,
],
];
$wechat_order = [
'out_trade_no' => $order['order_sn'],
'out_refund_no' => $order['order_sn'],
'total_fee' => $order['price']*100,
'refund_fee' => $order['price']*100
];
try {
$pay = Pay::wechat($config)->refund($wechat_order);
if ($pay->return_code != 'SUCCESS') {
$this->error($pay->return_msg);
}
} catch (Exception $e) {
$this->error($e->getMessage());
}

支付宝支付:

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
//调起支付(网站跳转支付)
$alipay_config = Config::get('alipay');
$config = [
'app_id' => $alipay_config['app_id'],
'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/portal/Order/alipay_notify',
'return_url' => 'https://'.$_SERVER['HTTP_HOST'].'/portal/Pay/pay_success',
'ali_public_key' => $alipay_config['ali_public_key'],
'private_key' => $alipay_config['private_key'],
'http' => [
'timeout' => 5.0,
'connect_timeout' => 5.0,
]
];
$pay_order = [
'out_trade_no' => $order['order_sn'],
'total_amount' => $order['price'],
'subject' => '测试订单',
];
try {
$alipay = Pay::alipay($config)->web($pay_order);
return $alipay->send();
} catch (\Exception $e) {
$this->error($e->getMessage());
}

//回调验签(buyer_id退款要用到,最好存起来)
$alipay = Pay::alipay($config);
$data = $alipay->verify();

$param = $data->all();
if (isset($param['out_trade_no'])&&!empty($param['out_trade_no'])&&$param['trade_status']=='TRADE_SUCCESS') {
(new OrderService($param['out_trade_no'], 'alipay', $param['buyer_id']))->call_notice();
}

//退款(用回原来的配置就行)
$alipay_order = [
'out_trade_no' => $order['order_sn'],
'refund_amount' => $order['price'],
'goods_id' => time(),
'goods_name' => '测试商品',
'quantity' => 1,
'price' => $order['price'],
'trans_in' => $order['buyer_id'],
];
try{
$alipay = Pay::alipay($config)->refund($alipay_order);
if ($alipay->code!='10000'||$alipay->buyer_user_id!=$order['buyer_id']||$alipay->out_trade_no!=$order['order_sn']) {
$this->error($alipay->msg);
}
} catch (Exception $e) {
$this->error($e->getMessage());
}