$wechatserviceconfig = [
'app_id' => '',//服务商的APPID
'sub_appid' =>'' ,//服务商子APPID
'mch_id' => '',//服务商的商户号
'sub_mch_id' => '',//服务商的子商户号
'key' => '',//服务商的秘钥
'cert_path'=> VENDOR_PATH.'cert/apiclient_cert.pem',
'key_path'=> VENDOR_PATH.'cert/apiclient_key.pem',
'secret' => '',//服务商的子secret
'notify_url' => ''
];
$this->wechatservice= Factory::payment($wechatserviceconfig);
$unify = $this->wechatservice->order->unify([
'body' => '报修订单',
'out_trade_no' => '',
'total_fee' => $prices*100,
'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
// 支付结果通知网址,如果不设置则会使用配置里的默认地址
'notify_url' => base_url() . 'api/Notify/payRepairNotify',
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'sub_openid' => $user['openid'],
]);
if($unify['result_code']=='SUCCESS'){
$result=$this->wechatservice->jssdk->bridgeConfig($unify['prepay_id'], false);
$this->success('提交成功', $result);
}else{
$this->error($unify['err_code_des']);
}
/**
* @desc 出入证支付回调
*/
public function payRenovationNotify(){
$response = $this->wechatservice->handlePaidNotify(function ($result, $fail) {
write_log($result, '../runtime/log/');
$order_no = $result['out_trade_no'];
if ($result['result_code'] === 'SUCCESS') {
Db::startTrans();
try {
$renovation = Renovation::where(['rno' => $order_no])->find();
//订单状态是未支付的才处理
if (empty($renovation['pay_status'])) {
$data = [
'pay_status' => 1,
'pay_time' => time(),
'transaction_id' => $result['transaction_id'],
];
Renovation::where(['rno' => $order_no])->update($data);
}
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
write_log($e, '../runtime/log/');
//服务器处理出现异常需要返回给微信false,让他再次发起通知
return $fail('服务器处理失败,请稍后再通知我');
}
} else if ($result['result_code'] === 'FAIL') {
//支付失败也需要返回true,表示服务器已处理,不然微信会反复通知,没有意义
return true;
}
});
$response->send();
}
if (!function_exists('base_url')) {
/**
* 获取当前域名及根路径
* @return string
*/
function base_url()
{
static $baseUrl = '';
if (empty($baseUrl)) {
$request = Request::instance();
$subDir = str_replace('\\', '/', dirname($request->server('PHP_SELF')));
$baseUrl = $request->scheme() . '://' . $request->host() . $subDir . ($subDir === '/' ? '' : '/');
}
return $baseUrl;
}
}
if (!function_exists('write_log')) {
/**
* 写入日志
* @param string|array $values
* @param string $dir
* @return bool|int
*/
function write_log($values, $dir){
if (is_array($values))
$values = print_r($values, true);
// 日志内容
$content = '['.date('Y-m-d H:i:s').']'.PHP_EOL.$values.PHP_EOL.PHP_EOL;
try {
// 文件路径
$filePath = $dir.date('Ym').'/';
// 路径不存在则创建
!is_dir($filePath) && mkdir($filePath, 0755, true);
// 写入文件
return file_put_contents($filePath.date('Ymd').'.log', $content, FILE_APPEND);
} catch (\Exception $e) {
return false;
}
}
}
/**
* 在线支付原路退款
*/
public function orderRefund($ids = NULL){
$refund = $this->model->get($ids);
if(empty($refund) || $refund->refundStatus==2) $this->error("该退款订单不存在或已退款!");
$order = \app\common\model\mall\order\Order::get($refund->order_id);
if(in_array($order->status,["canceled"]) || $order->refundis==0) {
$this->error("无效的退款订单!");
}
$this->model->transaction(function () use ($refund,$order) {
if($order->realTotalMoney==$refund->backMoney){
$refundResult=$this->wechatservice->refund
->byTransactionId($order['tradeNo'],$order['orderNo'],$order['realTotalMoney']*100,
$refund->backMoney*100);
if(!empty($refundResult)&&$refundResult['result_code']=='SUCCESS'){
//修改退款单信息
$refund->refundTime = time();
$refund->refundStatus = 2;
$refund->refundTradeNo = $refundResult['refund_id'];
$result=$refund->save();
//发送一条用户信息
if(!empty($result)){
User::money($refund->backMoney,$order->user_id,
'订单【'.$order->orderNo.'】退款¥'.$refund->backMoney."。");
$this->success('退款成功');
}
}else{
$this->success($refundResult['err_code_des']);
}
}else{
$this->success('退款金额有误');
}
});
}