PayController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace openend\controllers;
  3. use common\components\weixinUtil;
  4. use common\components\configDict;
  5. use common\components\stringUtil;
  6. use common\components\util;
  7. use Yii;
  8. use yii\web\Controller;
  9. use common\services\xhApplyOrderService;
  10. use common\services\xhInviteService;
  11. use common\services\xhMerchantService;
  12. use common\services\xhWxOpenService;
  13. class PayController extends OpenendController
  14. {
  15. public $withoutLogin = [];
  16. public function actionApply()
  17. {
  18. $get = Yii::$app->request->get();
  19. $orderId = isset($get['orderId']) ? $get['orderId'] : '';
  20. $apply = xhApplyOrderService::getById($orderId);
  21. $now = time();
  22. if(empty($apply)){
  23. util::stop('出错了');
  24. }
  25. if($apply['payStatus'] == 1){
  26. $this->redirect(['main/success']);
  27. util::end();
  28. }
  29. if($apply['userId'] != $this->userId){
  30. util::stop('非法访问');
  31. }
  32. $price = $apply['actPrice'];
  33. $merchantId = $apply['merchantId'];
  34. $merchant = xhMerchantService::getById($merchantId);
  35. return $this->renderPartial('apply',['apply'=>$apply,'price'=>$price,'merchant'=>$merchant]);
  36. }
  37. public function actionApplyGetParams()
  38. {
  39. ini_set('date.timezone','Asia/Shanghai');
  40. $post = Yii::$app->request->post();
  41. unset($post['_csrf']);
  42. $orderId = isset($post['orderId']) ? $post['orderId'] : '';
  43. $now = time();
  44. $expireTime = $now + 300;//订单5分钟后过期
  45. $payment = xhApplyOrderService::getById($orderId);
  46. if(empty($payment)){
  47. util::stop('订单不存在');
  48. }
  49. $name = '年服务费';
  50. $totalFee = $payment['actPrice'];
  51. $invitedMerchantId = $payment['invitedMerchantId'];
  52. $merchant = xhMerchantService::getById($invitedMerchantId);
  53. $openMerchant = xhWxOpenService::getMerchant();
  54. $openMerchantAccount = $openmerchant['id'];
  55. if(empty($merchant)){
  56. util::stop('商家信息未创建成功');
  57. }
  58. $inviteCode = $payment['inviteCode'];
  59. $invite = xhInviteService::getByCode($inviteCode);
  60. if(!empty($invite)){
  61. if($invite['takeStatus'] == 1){
  62. util::stop('您的邀请码已使用');
  63. }
  64. if($now > $invite['deadline']){
  65. util::stop('您的邀请码已失效');
  66. }
  67. }
  68. if($this->isWeixin == false){
  69. util::failInfo('请使用微信访问');
  70. }
  71. $userId = $this->userId;
  72. $user = $this->user;
  73. $openId = $user['openId'];
  74. $typeList = configDict::getConfig('capitalType');
  75. $capitalType = $typeList['xhApplyOrder']['id'];
  76. $attach = 'capitalType='.$capitalType.'&couponId=0';//将流水类型、代金劵传过去
  77. $weixin = Yii::getAlias("@vendor/weixin");
  78. require_once($weixin.'/lib/WxPay.Api.php');
  79. require_once($weixin.'/example/WxPay.JsApiPay.php');
  80. $input = new \WxPayUnifiedOrder();
  81. $input->SetBody($name);
  82. $input->SetOut_trade_no($orderId);
  83. $input->SetTotal_fee($totalFee*100);
  84. $input->SetTime_start(date("YmdHis",$now));
  85. $input->SetAttach($attach);
  86. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  87. $input->SetNotify_url(Yii::$app->params['openUrl'].'/notice/apply-weixin-callback/');
  88. $input->SetTrade_type("JSAPI");
  89. //服务商 代设置微信支付,要添加的参数设置
  90. if($this->merchantExtend['generalMerchant'] == 1){
  91. //$input->SetOpenid($openId);
  92. $input->SetSub_openid($openId);
  93. //自设置 微信支付
  94. }else{
  95. $input->SetOpenid($openId);
  96. }
  97. $wxOrder = \WxPayApi::unifiedOrder($input,6,$this->merchantExtend);
  98. $tools = new \JsApiPay();
  99. $jsApiParameters = $tools->GetJsApiParameters($wxOrder,$this->merchantExtend);
  100. $newParams = json_decode($jsApiParameters,true);
  101. $newParams['orderId'] = $orderId;
  102. $newParams['account'] = $openMerchantAccount;
  103. $newParams['newAccount'] = urlencode(stringUtil::authcode($merchant['id'],'ENCODE',300));
  104. util::successInfo('操作成功','A0001',$newParams);
  105. }
  106. public function actionApplySuccess()
  107. {
  108. return $this->renderPartial('applySuccess');
  109. }
  110. }