格式化代码

This commit is contained in:
liaofei 2019-11-20 16:43:24 +08:00
parent c48bdfa66a
commit ee35053c61

View File

@ -19,41 +19,38 @@ use app\models\user\UserRecharge;
* Class WechatMinService * Class WechatMinService
* @package service * @package service
*/ */
class MiniProgramService implements ProviderInterface class MiniProgramService
{ {
private static $instance = null; private static $instance = null;
public function register($config)
{
return ['mini_program',new self()];
}
public static function options() public static function options()
{ {
$wechat = SystemConfigService::more(['site_url','routine_appId','routine_appsecret']); $wechat = SystemConfigService::more(['site_url', 'routine_appId', 'routine_appsecret']);
$payment = SystemConfigService::more(['pay_routine_mchid','pay_routine_key','pay_routine_client_cert','pay_routine_client_key','pay_weixin_open']); $payment = SystemConfigService::more(['pay_routine_mchid', 'pay_routine_key', 'pay_routine_client_cert', 'pay_routine_client_key', 'pay_weixin_open']);
$config = []; $config = [];
$config['mini_program'] = [ $config['mini_program'] = [
'app_id'=>isset($wechat['routine_appId']) ? trim($wechat['routine_appId']):'', 'app_id' => isset($wechat['routine_appId']) ? trim($wechat['routine_appId']) : '',
'secret'=>isset($wechat['routine_appsecret']) ? trim($wechat['routine_appsecret']):'', 'secret' => isset($wechat['routine_appsecret']) ? trim($wechat['routine_appsecret']) : '',
'token'=>isset($wechat['wechat_token']) ? trim($wechat['wechat_token']):'', 'token' => isset($wechat['wechat_token']) ? trim($wechat['wechat_token']) : '',
'aes_key'=> isset($wechat['wechat_encodingaeskey']) ? trim($wechat['wechat_encodingaeskey']):'' 'aes_key' => isset($wechat['wechat_encodingaeskey']) ? trim($wechat['wechat_encodingaeskey']) : ''
]; ];
$config['payment'] = [ $config['payment'] = [
'app_id'=>isset($wechat['routine_appId']) ? trim($wechat['routine_appId']) :'', 'app_id' => isset($wechat['routine_appId']) ? trim($wechat['routine_appId']) : '',
'merchant_id'=>trim($payment['pay_routine_mchid']), 'merchant_id' => trim($payment['pay_routine_mchid']),
'key'=>trim($payment['pay_routine_key']), 'key' => trim($payment['pay_routine_key']),
'cert_path'=>realpath('.'.$payment['pay_routine_client_cert']), 'cert_path' => realpath('.' . $payment['pay_routine_client_cert']),
'key_path'=>realpath('.'.$payment['pay_routine_client_key']), 'key_path' => realpath('.' . $payment['pay_routine_client_key']),
'notify_url'=>$wechat['site_url'].Url::buildUrl('/api/routine/notify') 'notify_url' => $wechat['site_url'] . Url::buildUrl('/api/routine/notify')
]; ];
return $config; return $config;
} }
public static function application($cache = false) public static function application($cache = false)
{ {
(self::$instance === null || $cache === true) && (self::$instance = new Application(self::options())); (self::$instance === null || $cache === true) && (self::$instance = new Application(self::options()));
return self::$instance; return self::$instance;
} }
/** /**
* 小程序接口 * 小程序接口
* @return \EasyWeChat\MiniProgram\MiniProgram * @return \EasyWeChat\MiniProgram\MiniProgram
@ -81,7 +78,8 @@ class MiniProgramService implements ProviderInterface
* @param $encryptData * @param $encryptData
* @return $userInfo * @return $userInfo
*/ */
public static function encryptor($sessionKey, $iv, $encryptData){ public static function encryptor($sessionKey, $iv, $encryptData)
{
return self::miniprogram()->encryptor->decryptData($sessionKey, $iv, $encryptData); return self::miniprogram()->encryptor->decryptData($sessionKey, $iv, $encryptData);
} }
@ -123,7 +121,7 @@ class MiniProgramService implements ProviderInterface
*/ */
public static function appCodeUnlimitService($scene, $page = null, $width = 430, $autoColor = false, $lineColor = ['r' => 0, 'g' => 0, 'b' => 0]) public static function appCodeUnlimitService($scene, $page = null, $width = 430, $autoColor = false, $lineColor = ['r' => 0, 'g' => 0, 'b' => 0])
{ {
return self::qrcodeService()->appCodeUnlimit($scene,$page,$width,$autoColor,$lineColor); return self::qrcodeService()->appCodeUnlimit($scene, $page, $width, $autoColor, $lineColor);
} }
@ -144,12 +142,12 @@ class MiniProgramService implements ProviderInterface
* @param null $defaultColor * @param null $defaultColor
* @return mixed * @return mixed
*/ */
public static function sendTemplate($openid,$templateId,array $data,$form_id,$link = null,$defaultColor = null) public static function sendTemplate($openid, $templateId, array $data, $form_id, $link = null, $defaultColor = null)
{ {
$notice = self::noticeService()->to($openid)->template($templateId)->formId($form_id)->andData($data); $notice = self::noticeService()->to($openid)->template($templateId)->formId($form_id)->andData($data);
$message = []; $message = [];
if($link !== null) $message = ['page'=>$link]; if ($link !== null) $message = ['page' => $link];
if($defaultColor !== null) $notice->defaultColor($defaultColor); if ($defaultColor !== null) $notice->defaultColor($defaultColor);
return $notice->send($message); return $notice->send($message);
} }
@ -175,11 +173,11 @@ class MiniProgramService implements ProviderInterface
* @param array $options * @param array $options
* @return Order * @return Order
*/ */
protected static function paymentOrder($openid,$out_trade_no,$total_fee,$attach,$body,$detail='',$trade_type='JSAPI',$options = []) protected static function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
{ {
$total_fee = bcmul($total_fee,100,0); $total_fee = bcmul($total_fee, 100, 0);
$order = array_merge(compact('openid','out_trade_no','total_fee','attach','body','detail','trade_type'),$options); $order = array_merge(compact('openid', 'out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
if($order['detail'] == '') unset($order['detail']); if ($order['detail'] == '') unset($order['detail']);
return new Order($order); return new Order($order);
} }
@ -195,21 +193,22 @@ class MiniProgramService implements ProviderInterface
* @param array $options * @param array $options
* @return mixed * @return mixed
*/ */
public static function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail='', $trade_type='JSAPI', $options = []) public static function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
{ {
$order = self::paymentOrder($openid,$out_trade_no,$total_fee,$attach,$body,$detail,$trade_type,$options); $order = self::paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
$result = self::paymentService()->prepare($order); $result = self::paymentService()->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){ if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
try{ try {
PaymentRepositories::wechatPaymentPrepareProgram($order, $result->prepay_id); PaymentRepositories::wechatPaymentPrepareProgram($order, $result->prepay_id);
}catch (\Exception $e){} } catch (\Exception $e) {
}
return $result->prepay_id; return $result->prepay_id;
}else{ } else {
if($result->return_code == 'FAIL'){ if ($result->return_code == 'FAIL') {
exception('微信支付错误返回:'.$result->return_msg); exception('微信支付错误返回:' . $result->return_msg);
}else if(isset($result->err_code)){ } else if (isset($result->err_code)) {
exception('微信支付错误返回:'.$result->err_code_des); exception('微信支付错误返回:' . $result->err_code_des);
}else{ } else {
exception('没有获取微信支付的预支付ID请重新发起支付!'); exception('没有获取微信支付的预支付ID请重新发起支付!');
} }
exit; exit;
@ -229,9 +228,9 @@ class MiniProgramService implements ProviderInterface
* @param array $options * @param array $options
* @return array|string * @return array|string
*/ */
public static function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail='', $trade_type='JSAPI', $options = []) public static function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
{ {
return self::paymentService()->configForJSSDKPayment(self::paymentPrepare($openid,$out_trade_no,$total_fee,$attach,$body,$detail,$trade_type,$options)); return self::paymentService()->configForJSSDKPayment(self::paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options));
} }
/** /**
@ -245,11 +244,11 @@ class MiniProgramService implements ProviderInterface
* @param string $type * @param string $type
* @param string $refundAccount * @param string $refundAccount
*/ */
public static function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '' , $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS') public static function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
{ {
$totalFee = floatval($totalFee); $totalFee = floatval($totalFee);
$refundFee = floatval($refundFee); $refundFee = floatval($refundFee);
return self::paymentService()->refund($orderNo,$refundNo,$totalFee,$refundFee,$opUserId,$type,$refundAccount,$refundReason); return self::paymentService()->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $type, $refundAccount, $refundReason);
} }
/** 根据订单号退款 /** 根据订单号退款
@ -259,9 +258,9 @@ class MiniProgramService implements ProviderInterface
*/ */
public static function payOrderRefund($orderNo, array $opt) public static function payOrderRefund($orderNo, array $opt)
{ {
if(!isset($opt['pay_price'])) exception('缺少pay_price'); if (!isset($opt['pay_price'])) exception('缺少pay_price');
$totalFee = floatval(bcmul($opt['pay_price'],100,0)); $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
$refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'],100,0)) : null; $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
$refundReason = isset($opt['desc']) ? $opt['desc'] : ''; $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
$refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo; $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
$opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null; $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
@ -270,11 +269,11 @@ class MiniProgramService implements ProviderInterface
REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款) REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/ REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
$refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS'; $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
try{ try {
$res = (self::refund($orderNo,$refundNo,$totalFee,$refundFee,$opUserId,$refundReason,$type,$refundAccount)); $res = (self::refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount));
if($res->return_code == 'FAIL') exception('退款失败:'.$res->return_msg); if ($res->return_code == 'FAIL') exception('退款失败:' . $res->return_msg);
if(isset($res->err_code)) exception('退款失败:'.$res->err_code_des); if (isset($res->err_code)) exception('退款失败:' . $res->err_code_des);
}catch (\Exception $e){ } catch (\Exception $e) {
exception($e->getMessage()); exception($e->getMessage());
} }
return true; return true;
@ -285,24 +284,24 @@ class MiniProgramService implements ProviderInterface
*/ */
public static function handleNotify() public static function handleNotify()
{ {
self::paymentService()->handleNotify(function($notify, $successful){ self::paymentService()->handleNotify(function ($notify, $successful) {
if($successful && isset($notify->out_trade_no)){ if ($successful && isset($notify->out_trade_no)) {
if(isset($notify->attach) && $notify->attach){ if (isset($notify->attach) && $notify->attach) {
if(($count = strpos($notify->out_trade_no,'_')) !== false){ if (($count = strpos($notify->out_trade_no, '_')) !== false) {
$notify->out_trade_no = substr($notify->out_trade_no,$count+1); $notify->out_trade_no = substr($notify->out_trade_no, $count + 1);
} }
if(strtolower($notify->attach) == 'productr'){//TODO 商品订单支付成功后 if (strtolower($notify->attach) == 'productr') {//TODO 商品订单支付成功后
try{ try {
if(StoreOrderRoutineModel::be(['order_id'=>$notify->out_trade_no,'paid'=>1])) return true; if (StoreOrderRoutineModel::be(['order_id' => $notify->out_trade_no, 'paid' => 1])) return true;
return StoreOrderRoutineModel::paySuccess($notify->out_trade_no); return StoreOrderRoutineModel::paySuccess($notify->out_trade_no);
}catch (\Exception $e){ } catch (\Exception $e) {
return false; return false;
} }
}else if(strtolower($notify->attach) == 'user_recharge'){ } else if (strtolower($notify->attach) == 'user_recharge') {
try{ try {
if(UserRecharge::be(['order_id'=>$notify->out_trade_no,'paid'=>1])) return true; if (UserRecharge::be(['order_id' => $notify->out_trade_no, 'paid' => 1])) return true;
return UserRecharge::rechargeSuccess($notify->out_trade_no); return UserRecharge::rechargeSuccess($notify->out_trade_no);
}catch (\Exception $e){ } catch (\Exception $e) {
return false; return false;
} }
} }
@ -327,6 +326,4 @@ class MiniProgramService implements ProviderInterface
} }
} }