YoppayController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/12/22
  6. * Time: 9:51
  7. */
  8. namespace app\payment\controllers;
  9. use app\payment\PaymentController;
  10. use app\common\helpers\Url;
  11. use Illuminate\Support\Facades\DB;
  12. use app\common\models\AccountWechats;
  13. use app\common\services\Pay;
  14. use Yunshop\YopPay\models\YopOrderRefund;
  15. use Yunshop\YopPay\models\YopPayOrder;
  16. class YoppayController extends PaymentController
  17. {
  18. protected $set;
  19. protected $parameters;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->set = $this->getMerchantNo();
  24. if (empty($this->set)) {
  25. exit('商户不存在');
  26. }
  27. if (empty(\YunShop::app()->uniacid)) {
  28. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->set['uniacid'];
  29. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  30. }
  31. if (!app('plugins')->isEnabled('yop-pay')) {
  32. echo 'Not turned on yop pay';
  33. exit();
  34. }
  35. $this->dealWith();
  36. }
  37. protected function getMerchantNo()
  38. {
  39. $app_key = $_REQUEST['customerIdentification'];
  40. $merchant_no = substr($app_key, strrpos($app_key, 'OPR:')+4);
  41. $set = DB::table('yz_yop_setting')->where('merchant_no', $merchant_no)->first();
  42. return $set;
  43. }
  44. private function dealWith()
  45. {
  46. $yop_data = $_REQUEST['response'];
  47. if ($yop_data) {
  48. $response = \Yunshop\YopPay\common\Util\YopSignUtils::decrypt($yop_data, $this->set['son_private_key'], $this->set['yop_public_key']);
  49. $this->parameters = json_decode($response, true);
  50. }
  51. }
  52. //错误日志
  53. protected function yopLog($desc,$error,$data)
  54. {
  55. \Yunshop\YopPay\common\YopLog::yopLog($desc, $error,$data);
  56. }
  57. //返回日志
  58. protected function yopResponse($desc,$params, $type = 'unify')
  59. {
  60. \Yunshop\YopPay\common\YopLog::yopResponse($desc, $params, $type);
  61. }
  62. //返回日志
  63. protected function debug($desc, $params = [])
  64. {
  65. \Yunshop\YopPay\common\YopLog::debug($desc, $params);
  66. }
  67. //异步支付通知
  68. public function notifyUrl()
  69. {
  70. $this->log($this->set['merchant_no'], $this->parameters);
  71. $this->yopResponse('支付通知pay_sn:'.$this->getParameter('orderId'), $this->parameters, 'pay');
  72. $pay_order = YopPayOrder::paySn($this->getParameter('orderId'))->first();
  73. if (!$pay_order) {
  74. $this->savePayOrder();
  75. }
  76. $data = [
  77. 'total_fee' => floatval($this->getParameter('orderAmount')),
  78. 'out_trade_no' => $this->getParameter('orderId'),
  79. 'trade_no' => $this->getParameter('uniqueOrderNo'),
  80. 'unit' => 'yuan',
  81. 'pay_type' => '易宝微信',
  82. 'pay_type_id' => 26,
  83. ];
  84. //支付产品为用户扫码 支付类型为支付宝
  85. if ($this->paymentProduct() == YopPayOrder::SCCANPAY && $this->platformType() == YopPayOrder::TYPE_ALIPAY) {
  86. $data['pay_type'] = '易宝支付宝';
  87. $data['pay_type_id'] = 32;
  88. }
  89. $this->payResutl($data);
  90. echo 'SUCCESS';
  91. exit();
  92. }
  93. protected function savePayOrder()
  94. {
  95. $data = [
  96. 'uniacid'=> \YunShop::app()->uniacid,
  97. 'pay_sn' => $this->getParameter('orderId'),
  98. 'yop_order_no' => $this->getParameter('uniqueOrderNo'),
  99. 'order_amount' => $this->getParameter('orderAmount'),
  100. 'total_amount' => $this->getParameter('payAmount'),
  101. 'can_divide_amount' => $this->getParameter('payAmount'),
  102. 'rate' => $this->set['rate'],
  103. 'rate_amount' => $this->rateAmount(),
  104. 'pay_at' => strtotime($this->getParameter('paySuccessDate')),
  105. 'platform_type' => $this->platformType(),
  106. 'payment_product' => $this->paymentProduct(),
  107. ];
  108. $yop_order = new YopPayOrder();
  109. $yop_order->fill($data);
  110. $yop_order->save();
  111. }
  112. //平台分类 支付类型
  113. protected function platformType()
  114. {
  115. $status = 0;
  116. if (!empty($this->parameters['platformType'])) {
  117. switch ($this->parameters['platformType']) {
  118. case 'WECHAT': //微信
  119. $status = YopPayOrder::TYPE_WECHAT;
  120. break;
  121. case 'ALIPAY': //支付宝
  122. $status = YopPayOrder::TYPE_ALIPAY;
  123. break;
  124. case 'NET':
  125. $status = YopPayOrder::TYPE_NET;
  126. break;
  127. case 'NCPAY':
  128. $status = YopPayOrder::TYPE_NCPAY;
  129. break;
  130. case 'CFL':
  131. $status = YopPayOrder::TYPE_CFL;
  132. break;
  133. default:
  134. $status = 0;
  135. break;
  136. }
  137. }
  138. return $status;
  139. }
  140. //支付产品
  141. protected function paymentProduct()
  142. {
  143. $status = 0;
  144. if (!empty($this->parameters['paymentProduct'])) {
  145. switch ($this->parameters['paymentProduct']) {
  146. case 'WECHAT_OPENID': //微信公众号
  147. $status = YopPayOrder::WECHAT_OPENID;
  148. break;
  149. case 'SCCANPAY': //用户扫码
  150. $status = YopPayOrder::SCCANPAY;
  151. break;
  152. case 'MSCANPAY': //商家扫码
  153. $status = YopPayOrder::MSCANPAY;
  154. break;
  155. case 'ZFB_SHH': //支付宝生活号
  156. $status = YopPayOrder::ZFB_SHH;
  157. break;
  158. case 'ZF_ZHZF': //商户账户支付
  159. $status = YopPayOrder::ZF_ZHZF;
  160. break;
  161. case 'EWALLETH5': //钱包H5支付
  162. $status = YopPayOrder::EWALLETH5;
  163. break;
  164. default:
  165. $status = 0;
  166. break;
  167. }
  168. }
  169. return $status;
  170. }
  171. protected function rateAmount()
  172. {
  173. $rate = $this->set['rate'];
  174. $rate_amount = bcmul($this->getParameter('orderAmount'), bcdiv($rate, 100,4), 2);
  175. return max($rate_amount, 0);
  176. }
  177. //同步通知
  178. public function redirectUrl()
  179. {
  180. // $yop_data = $_REQUEST;
  181. $this->debug('<-----------易宝同步通知---------------->', $_REQUEST);
  182. //$url = str_replace('https','http', Url::shopSchemeUrl("?menu#/member/payYes?i={$uniacid}"));
  183. //redirect($url)->send();
  184. }
  185. //订单超时通知地址
  186. public function timeoutNotifyUrl()
  187. {
  188. $this->debug('<-------易宝支付订单超时通知-------------->', $this->parameters);
  189. }
  190. //订单清算通知地址
  191. public function csUrl()
  192. {
  193. $yop_order = YopPayOrder::csAnnal($this->getParameter('uniqueOrderNo'), $this->getParameter('orderId'))->first();
  194. if (!$yop_order) {
  195. $this->yopLog('订单清算','易宝支付订单不存在无法清算',$this->parameters);
  196. exit('Record does not exist');
  197. }
  198. $this->yopResponse('订单清算pay_sn:'.$this->getParameter('orderId'), $this->parameters, 'cs');
  199. $data = [
  200. 'status' => 1,
  201. 'cs_at' => strtotime($this->getParameter('csSuccessDate')),
  202. 'merchant_fee' => $this->getParameter('merchantFee'),
  203. 'customer_fee' => $this->getParameter('customerFee'),
  204. ];
  205. $yop_order->fill($data);
  206. $yop_order->save();
  207. echo 'SUCCESS';exit();
  208. }
  209. //订单退款
  210. public function refundUrl()
  211. {
  212. $this->debug('-------------易宝订单退款通知---------------');
  213. $yop_refund = YopOrderRefund::getRefundAnnal($this->getParameter('orderId'), $this->getParameter('refundRequestId'))
  214. ->where('status', YopOrderRefund::REFUND)->first();
  215. if (!$yop_refund) {
  216. $this->yopLog('退款不存在pay_sn:'.$this->getParameter('orderId'),'易宝订单退款记录不存在',$this->parameters);
  217. exit('Record does not exist');
  218. }
  219. if ($yop_refund->status != YopOrderRefund::REFUND) {
  220. $this->debug('<-----------退款已处理---------------->', ['id'=>$yop_refund->id, 'pay_sn'=>$this->getParameter('orderId')]);
  221. echo 'SUCCESS';exit();
  222. }
  223. $this->yopResponse('退款通知pay_sn:'.$this->getParameter('orderId'), $this->parameters, 'refund');
  224. $yop_refund->status = $this->refundStatus();
  225. if ($this->getParameter('refundSuccessDate')) {
  226. $yop_refund->refund_at = strtotime($this->getParameter('refundSuccessDate'));
  227. }
  228. if ($this->getParameter('errorMessage')) {
  229. $yop_refund->error_message = $this->getParameter('errorMessage');
  230. }
  231. $bool = $yop_refund->save();
  232. if ($bool) {
  233. $yop_order = YopPayOrder::paySn($this->getParameter('orderId'))->where('can_refund', YopOrderRefund::REFUND_APPLY)->first();
  234. if ($yop_order) {
  235. $yop_order->refund_amount = bcadd($yop_order->refund_amount, $this->getParameter('refundAmount'), 2);
  236. $yop_order->can_refund = YopOrderRefund::REFUND;
  237. $yop_order->save();
  238. }
  239. }
  240. echo 'SUCCESS';exit();
  241. }
  242. //支付产品
  243. protected function refundStatus()
  244. {
  245. $status = 0;
  246. if (!empty($this->parameters['status'])) {
  247. switch ($this->parameters['status']) {
  248. case 'FAILED':
  249. $status = YopOrderRefund::REFUND_FAILED;
  250. break;
  251. case 'SUCCESS':
  252. $status = YopOrderRefund::REFUND_SUCCESS;
  253. break;
  254. case 'CANCEL':
  255. $status = YopOrderRefund::REFUND_CANCEL;
  256. break;
  257. default:
  258. $status = 0;
  259. break;
  260. }
  261. }
  262. return $status;
  263. }
  264. /**
  265. *获取参数值
  266. */
  267. public function getParameter($parameter) {
  268. return isset($this->parameters[$parameter])?$this->parameters[$parameter] : '';
  269. }
  270. /**
  271. * 支付日志
  272. *
  273. * @param $post
  274. */
  275. public function log($out_trade_no, $data, $msg = '易宝支付')
  276. {
  277. //访问记录
  278. Pay::payAccessLog();
  279. //保存响应数据
  280. Pay::payResponseDataLog($out_trade_no, $msg, json_encode($data));
  281. }
  282. }