Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zuiketang
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
zuiketang
Commits
6443711d
Commit
6443711d
authored
Nov 03, 2016
by
杨培
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
微信API优化调整
完成结算计划任务
parent
f06409b3
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
1030 additions
and
1368 deletions
+1030
-1368
WxPayHelper.php
src/common/libs/WxPayHelper.php
+47
-0
WxPay.Api.php
src/common/libs/weixin/libs/WxPay.Api.php
+40
-2
WxPay.Config.php
src/common/libs/weixin/libs/WxPay.Config.php
+1
-1
WxPay.Data.php
src/common/libs/weixin/libs/WxPay.Data.php
+817
-1354
Income.php
src/common/models/Income.php
+9
-0
SettleAccountWeixin.php
src/common/models/SettleAccountWeixin.php
+11
-0
SettleOrders.php
src/common/models/SettleOrders.php
+40
-5
WithdrawController.php
src/console/controllers/WithdrawController.php
+64
-5
m161020_033521_settle_orders_init.php
src/console/migrations/m161020_033521_settle_orders_init.php
+1
-1
No files found.
src/common/libs/WxPayHelper.php
0 → 100644
View file @
6443711d
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 2016/11/3
* Time: 14:26
*/
namespace
common\libs
;
require_once
__DIR__
.
'/weixin/libs/WxPay.Data.php'
;
require_once
__DIR__
.
'/weixin/libs/WxPay.Api.php'
;
class
WxPayHelper
{
/**
* 企业支付
*
* @param string $openId 要支付到的微信用户OpenId
* @param string $outTradeNo 本地订单号
* @param float $amount 支付金额,单位元,最多精确到小数点两位
* @param string $clientIp 客户端IP
* @param string $desc 订单简介
* @param string $checkName 是否检查昵称
* @param string $reUserName 要检查的昵称
* @return array
*/
public
static
function
transfers
(
$openId
,
$outTradeNo
,
$amount
,
$clientIp
,
$desc
,
$checkName
=
'NO_CHECK'
,
$reUserName
=
''
)
{
$amount
=
bcmul
(
$amount
,
100
,
0
);
//微信内为分的计算方式,所以需要转换
$transfer
=
new
\WxPayTransfers
();
$transfer
->
setOpenId
(
$openId
);
$transfer
->
setAmount
(
$amount
);
$transfer
->
setCheckName
(
$checkName
);
$transfer
->
setMchBillNo
(
$outTradeNo
);
$transfer
->
setDesc
(
$desc
);
$transfer
->
setReuserName
(
$reUserName
);
$transfer
->
SetSpbill_create_ip
(
$clientIp
);
$result
=
\WxPayApi
::
transfers
(
$transfer
);
is_array
(
$result
)
&&
$result
[
'out_trade_no'
]
=
$outTradeNo
;
return
$result
;
}
}
\ No newline at end of file
src/common/libs/weixin/libs/WxPay.Api.php
View file @
6443711d
...
...
@@ -12,6 +12,42 @@ require_once "WxPay.Data.php";
*/
class
WxPayApi
{
/**
* 付款接口
*
* @param \WxPayTransfers $inputObj
* @param int $timeOut
* @return array
*/
public
static
function
transfers
(
$inputObj
,
$timeOut
=
6
)
{
$url
=
'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'
;
$inputObj
->
SetAppid
(
WxPayConfig
::
APPID
,
'appid'
);
$inputObj
->
SetMch_id
(
WxPayConfig
::
MCHID
);
$inputObj
->
SetNonce_str
(
self
::
getNonceStr
());
if
(
!
$inputObj
->
IsSpbill_create_ipSet
())
{
$inputObj
->
SetSpbill_create_ip
(
$_SERVER
[
'REMOTE_ADDR'
]);
}
$inputObj
->
SetSign
();
$xml
=
$inputObj
->
ToXml
();
$startTimeStamp
=
self
::
getMillisecond
();
$response
=
self
::
postXmlCurl
(
$xml
,
$url
,
true
,
$timeOut
);
$_ENV
[
'last_post_xml'
]
=
$xml
;
$result
=
new
WxPayResults
();
$result
->
FromXml
(
$response
);
$return
=
$result
->
GetValues
();
self
::
reportCostTime
(
$url
,
$startTimeStamp
,
$return
);
return
$return
;
}
/**
*
* 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
...
...
@@ -534,6 +570,7 @@ class WxPayApi
curl_setopt
(
$ch
,
CURLOPT_PROXYPORT
,
WxPayConfig
::
CURL_PROXY_PORT
);
}
curl_setopt
(
$ch
,
CURLOPT_URL
,
$url
);
//curl_setopt($ch,CURLOPT_VERBOSE,TRUE);
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYPEER
,
TRUE
);
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYHOST
,
2
);
//严格校验
//设置header
...
...
@@ -545,9 +582,10 @@ class WxPayApi
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt
(
$ch
,
CURLOPT_SSLCERTTYPE
,
'PEM'
);
curl_setopt
(
$ch
,
CURLOPT_SSLCERT
,
WxPayConfig
::
SSLCERT_PATH
);
curl_setopt
(
$ch
,
CURLOPT_SSLCERT
,
realpath
(
__DIR__
.
'/'
.
WxPayConfig
::
SSLCERT_PATH
)
);
curl_setopt
(
$ch
,
CURLOPT_SSLKEYTYPE
,
'PEM'
);
curl_setopt
(
$ch
,
CURLOPT_SSLKEY
,
WxPayConfig
::
SSLKEY_PATH
);
curl_setopt
(
$ch
,
CURLOPT_SSLKEY
,
realpath
(
__DIR__
.
'/'
.
WxPayConfig
::
SSLKEY_PATH
));
curl_setopt
(
$ch
,
CURLOPT_SSLCERTPASSWD
,
WxPayConfig
::
MCHID
);
}
//post提交方式
curl_setopt
(
$ch
,
CURLOPT_POST
,
TRUE
);
...
...
src/common/libs/weixin/libs/WxPay.Config.php
View file @
6443711d
...
...
@@ -34,7 +34,7 @@ class WxPayConfig
* API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书)
* @var path
*/
const
SSLCERT_PATH
=
'../cert/apiclient_cert.p
em
'
;
const
SSLCERT_PATH
=
'../cert/apiclient_cert.p
12
'
;
const
SSLKEY_PATH
=
'../cert/apiclient_key.pem'
;
//=======【curl代理设置】===================================
...
...
src/common/libs/weixin/libs/WxPay.Data.php
View file @
6443711d
This diff is collapsed.
Click to expand it.
src/common/models/Income.php
View file @
6443711d
...
...
@@ -165,4 +165,13 @@ class Income extends \yii\db\ActiveRecord
return
$query
->
asArray
()
->
all
();
}
public
static
function
updateSettleOk
(
$uid
,
$minId
,
$maxId
)
{
self
::
updateAll
([
'status'
=>
self
::
STATUS_IS_SETTLED
],
'user_id=:uid AND id BETWEEN :min AND :max'
,
[
':uid'
=>
$uid
,
':min'
=>
$minId
,
':max'
=>
$maxId
]);
}
}
src/common/models/SettleAccountWeixin.php
View file @
6443711d
...
...
@@ -92,5 +92,16 @@ class SettleAccountWeixin extends ActiveRecord
{
return
$size
==
self
::
HEAD_SIZE_ORG
?
$headImgUrl
:
substr_replace
(
$headImgUrl
,
$size
,
-
1
);
}
/**
* 获取绑定微信的基本信息
*
* @param int $uid
* @return array
*/
public
static
function
getBaseInfo
(
$uid
)
{
return
self
::
find
()
->
select
(
'user_id, open_id, nickname, headimgurl'
)
->
where
([
'user_id'
=>
$uid
])
->
asArray
()
->
one
();
}
}
src/common/models/SettleOrders.php
View file @
6443711d
<?php
namespace
common\models
;
use
yii\base\InvalidCallException
;
use
yii\behaviors\AttributeBehavior
;
use
yii\behaviors\TimestampBehavior
;
use
yii\db\ActiveRecord
;
...
...
@@ -12,7 +13,7 @@ use yii\db\ActiveRecord;
* @property integer $user_id
* @property string $type
* @property string $out_trade_no
* @property string $
trade
_no
* @property string $
payment
_no
* @property string $amount
* @property integer $income_id_min
* @property integer $income_id_max
...
...
@@ -50,12 +51,12 @@ class SettleOrders extends ActiveRecord
return
[
[[
'user_id'
,
'amount'
,
'income_id_min'
,
'income_id_max'
],
'required'
],
[[
'user_id'
,
'income_id_min'
,
'income_id_max'
,
'status'
,
'created_at'
,
'updated_at'
],
'integer'
],
[[
'type'
,
'out_trade_no'
,
'
trade
_no'
],
'string'
],
[[
'type'
,
'out_trade_no'
,
'
payment
_no'
],
'string'
],
[[
'type'
],
'default'
,
'value'
=>
self
::
TYPE_WEIXIN
],
[[
'amount'
],
'number'
],
[[
'out_trade_no'
,
'
trade
_no'
],
'string'
,
'max'
=>
50
],
[[
'out_trade_no'
,
'
payment
_no'
],
'string'
,
'max'
=>
50
],
[[
'out_trade_no'
],
'unique'
],
[[
'
trade
_no'
],
'default'
,
'value'
=>
''
],
[[
'
payment
_no'
],
'default'
,
'value'
=>
''
],
[[
'status'
],
'default'
,
'value'
=>
self
::
STATUS_UNPAID
]
];
}
...
...
@@ -70,7 +71,7 @@ class SettleOrders extends ActiveRecord
'user_id'
=>
'User ID'
,
'type'
=>
'Type'
,
'out_trade_no'
=>
'Out Trade No'
,
'
trade_no'
=>
'Trade
No'
,
'
payment_no'
=>
'Payment
No'
,
'amount'
=>
'Amount'
,
'income_id_min'
=>
'Income Id Min'
,
'income_id_max'
=>
'Income Id Max'
,
...
...
@@ -120,6 +121,12 @@ class SettleOrders extends ActiveRecord
return
$this
->
out_trade_no
;
}
/**
* 生成本地订单
*
* @param int $userId
* @return string
*/
public
static
function
generateOutTradeNo
(
$userId
)
{
$userSeg
=
sprintf
(
'%08d'
,
$userId
);
...
...
@@ -149,4 +156,32 @@ class SettleOrders extends ActiveRecord
return
self
::
find
()
->
where
([
'status'
=>
self
::
STATUS_UNPAID
])
->
asArray
()
->
all
();
}
/**
* 更新支付单为成功状态
*
* @param int $id
* @param string $paymentNo
* @return int
*/
public
static
function
updateStatusPaid
(
$id
,
$paymentNo
)
{
return
self
::
updateAll
([
'payment_no'
=>
$paymentNo
,
'status'
=>
self
::
STATUS_PAIED
],
[
'id'
=>
$id
]);
}
/**
* 更新指定订单状态
*
* @param int $id
* @param int $status
* @return int
*/
public
static
function
updateStatus
(
$id
,
$status
)
{
if
(
$status
==
self
::
STATUS_PAIED
)
{
throw
new
InvalidCallException
(
'Unable to update status paid, use updateStatusPaid() instated.'
);
}
return
self
::
updateAll
([
'status'
=>
$status
],
[
'id'
=>
$id
]);
}
}
src/console/controllers/WithdrawController.php
View file @
6443711d
...
...
@@ -9,10 +9,13 @@
namespace
console\controllers
;
use
common\libs\Utils
;
use
common\libs\WxPayHelper
;
use
common\models\Income
;
use
common\models\Orders
;
use
common\models\SettleAccountWeixin
;
use
common\models\SettleOrders
;
use
common\models\User
;
use
Yii
;
use
yii\console\Controller
;
/**
...
...
@@ -39,6 +42,11 @@ class WithdrawController extends Controller
$count
=
0
;
foreach
(
$incomeData
as
$row
)
{
//最低金额1元起付
if
(
$row
[
'amount'
]
<
1
)
{
continue
;
}
$order
=
new
SettleOrders
();
$order
->
load
(
$row
);
...
...
@@ -46,12 +54,12 @@ class WithdrawController extends Controller
$sameOrder
=
SettleOrders
::
findSameOrder
(
$order
->
user_id
,
$order
->
income_id_min
,
$order
->
income_id_max
);
if
(
$sameOrder
)
{
\
Yii
::
warning
(
"Create settle order canceled because found same order:
{
id={$sameOrder['id']
}
, out_trade_no=
{
$sameOrder
[
'out_trade_no'
]
}
}."
);
Yii
::
warning
(
"Create settle order canceled because found same order:
{
id={$sameOrder['id']
}
, out_trade_no=
{
$sameOrder
[
'out_trade_no'
]
}
}."
);
continue
;
}
if
(
!
$order
->
save
())
{
\
Yii
::
error
(
'Create settle order failed:'
.
Utils
::
fetchErrorFirst
(
$order
->
errors
));
Yii
::
error
(
'Create settle order failed:'
.
Utils
::
fetchErrorFirst
(
$order
->
errors
));
continue
;
}
...
...
@@ -60,16 +68,67 @@ class WithdrawController extends Controller
$info
=
"Create settle orders finish, count=
$count
."
;
\
Yii
::
info
(
$info
);
Yii
::
info
(
$info
);
echo
$info
.
"
\n
"
;
}
/**
* @return void
*/
public
function
actionPaySettleOrders
()
{
$unpaidOrders
=
SettleOrders
::
getAllUnpaidOrders
();
if
(
!
$unpaidOrders
)
{
$info
=
"No unpaid settle orders."
;
Yii
::
info
(
$info
);
echo
$info
.
"
\n
"
;
return
;
}
$success
=
$failed
=
0
;
foreach
(
$unpaidOrders
as
$order
)
{
//先锁定
SettleOrders
::
updateStatus
(
$order
[
'id'
],
SettleOrders
::
STATUS_ONPAY
);
$account
=
SettleAccountWeixin
::
getBaseInfo
(
$order
[
'user_id'
]);
if
(
!
$account
)
{
++
$failed
;
Yii
::
warning
(
"Pay settle order ignore because no found weixin account:
{
id={$order['id']
}
, user_id=
{
$order
[
'user_id'
]
}
}."
);
continue
;
}
//获取用户IP
$user
=
User
::
find
()
->
select
(
'ip'
)
->
where
([
'id'
=>
$order
[
'user_id'
]])
->
asArray
()
->
one
();
//发起付款
$result
=
WxPayHelper
::
transfers
(
$account
[
'open_id'
],
$order
[
'out_trade_no'
],
$order
[
'amount'
],
$user
[
'ip'
],
'收入结算-'
.
$order
[
'id'
]);
if
(
!
isset
(
$result
[
'return_code'
])
||
!
isset
(
$result
[
'result_code'
]))
{
//接口调用失败恢复为未支付状态
SettleOrders
::
updateStatus
(
$order
[
'id'
],
SettleOrders
::
STATUS_UNPAID
);
++
$failed
;
$json
=
json_encode
(
$result
,
JSON_UNESCAPED_UNICODE
);
Yii
::
error
(
"Pay settle order failed:
{
id={$order['id']
}
, user_id=
{
$order
[
'user_id'
]
}
, out_trade_no=
{
$order
[
'out_trade_no'
]
}
}
\n
$json
"
);
}
elseif
(
$result
[
'return_code'
]
==
'SUCCESS'
&&
$result
[
'result_code'
]
==
'SUCCESS'
)
{
//支付成功
SettleOrders
::
updateStatusPaid
(
$order
[
'id'
],
$result
[
'payment_no'
]);
Income
::
updateSettleOk
(
$order
[
'user_id'
],
$order
[
'income_id_min'
],
$order
[
'income_id_max'
]);
++
$success
;
}
else
{
SettleOrders
::
updateStatus
(
$order
[
'id'
],
SettleOrders
::
STATUS_FAILED
);
++
$failed
;
Yii
::
error
(
"Pay settle order failed:
{
$result
[
'return_msg'
]
}
\n
{
id={$order['id']
}
, user_id=
{
$order
[
'user_id'
]
}
, out_trade_no=
{
$order
[
'out_trade_no'
]
}
}"
);
}
}
print_r
(
$unpaidOrders
);
$info
=
"Pay settle orders finish, success=
$success
, failed=
$failed
"
;
Yii
::
info
(
$info
);
echo
$info
.
"
\n
"
;
}
}
\ No newline at end of file
src/console/migrations/m161020_033521_settle_orders_init.php
View file @
6443711d
...
...
@@ -13,7 +13,7 @@ class m161020_033521_settle_orders_init extends Migration
'user_id'
=>
$this
->
integer
(
10
)
->
unsigned
()
->
notNull
(),
'`type` enum(\'weixin\',\'alipay\') NOT NULL COMMENT \'结算类型\''
,
'out_trade_no'
=>
$this
->
string
(
50
)
->
comment
(
'本地订单编号'
),
'
trade_no'
=>
$this
->
string
(
50
)
->
comment
(
'支付平台订单编
号'
),
'
payment_no'
=>
$this
->
string
(
50
)
->
comment
(
'支付平台单
号'
),
'amount'
=>
$this
->
decimal
(
8
,
2
)
->
unsigned
()
->
comment
(
'结算金额'
),
'income_id_min'
=>
$this
->
integer
(
10
)
->
unsigned
()
->
comment
(
'结算的收记录范围最小id'
),
'income_id_max'
=>
$this
->
integer
(
10
)
->
unsigned
()
->
comment
(
'结算的收记录范围最大id'
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment