SilverPointPayController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/7/4
  8. * Time: 16:12
  9. */
  10. namespace app\payment\controllers;
  11. use app\common\models\PayOrder;
  12. use app\common\models\PayType;
  13. use app\common\models\PayWithdrawOrder;
  14. use app\common\models\Withdraw;
  15. use app\payment\PaymentController;
  16. use Yunshop\SilverPointPay\models\ProfitShare;
  17. use Yunshop\SilverPointPay\models\WithdrawRelation;
  18. use Yunshop\SilverPointPay\services\SilverPointPay;
  19. use app\common\helpers\Url;
  20. class SilverPointPayController extends PaymentController
  21. {
  22. protected $uniacid;
  23. public function notifyUrlPay()
  24. {
  25. $this->debugMsg('接受银典异步回调');
  26. $result = $this->getNotifyRequest();
  27. //增加是否是代付验证,因为银典逻辑问题,此处做兼容
  28. if ($result['type_num'] == 1108) {
  29. return $this->withdraw($result);
  30. }
  31. if ($result['status']) {
  32. $res = $this->processOrder($result);
  33. if ($res) {
  34. return $this->successReply();
  35. }
  36. }
  37. }
  38. /**
  39. * 1108"说明是代付订单,目前只有余额提现、收入提现使用代付打款
  40. *
  41. * 银典支付-代付逻辑,如果配置了回调地址,优先使用配置回调地址,所以不能直接使用withdrawNotifyUrl
  42. *
  43. * 这个改用 notifyUrlPay 方法,根据 txnNum == 1108 验证是银典支付-代付打款逻辑(提现打款)
  44. */
  45. public function withdraw($params)
  46. {
  47. $this->debugMsg('接受银典支付-代付异步回调');
  48. if ($params['status'] == 1) {
  49. $pay_refund_model = PayWithdrawOrder::getOrderInfo($params['out_order_no']);
  50. if ($pay_refund_model) {
  51. $pay_refund_model->status = 2;
  52. $pay_refund_model->trade_no = $params['out_order_no'];
  53. $pay_refund_model->save();
  54. \app\common\services\finance\Withdraw::paySuccess($params['out_order_no']);
  55. return $this->successReply();
  56. }
  57. \Log::debug('银典支付-代付', 'withdraw.succeeded');
  58. } else {
  59. $pay_refund_model = PayWithdrawOrder::getOrderInfo($params['out_order_no']);
  60. if ($pay_refund_model) {
  61. \Log::debug('银典支付-代付', 'withdraw.failed');
  62. \app\common\services\finance\Withdraw::payFail($params['out_order_no']);
  63. $withdrawId = Withdraw::where('withdraw_sn', $params['out_order_no'])->first()->id;
  64. WithdrawRelation::where('withdraw_id', $withdrawId)->update(['status' => -1]);
  65. }
  66. }
  67. }
  68. public function frontUrlUnionPay()
  69. {
  70. $request = request()->all();
  71. $order_info = unserialize($request['afford']);
  72. $this->uniacid = $order_info['i'];
  73. \YunShop::app()->uniacid = $this->uniacid;
  74. \Setting::$uniqueAccountId = $this->uniacid;
  75. $silverPointPay = new SilverPointPay();
  76. $respData = $silverPointPay->queryTradeNo($order_info['order_no']);
  77. $effective = false;
  78. if ($respData["respCode"] == '0000' && $respData["transStatus"] == '100') {
  79. $effective = true;
  80. }
  81. if ($effective) {
  82. redirect(Url::absoluteApp('member/payYes', ['i' => $order_info['i']]))->send();
  83. } else {
  84. redirect(Url::absoluteApp('member/payErr', ['i' => $order_info['i']]))->send();
  85. }
  86. }
  87. public function notifySplitBill()
  88. {
  89. $this->debugMsg('接受银典分账异步回调');
  90. $msg = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents(
  91. "php://input"
  92. );
  93. if (empty($msg)) {
  94. # 如果没有数据,直接返回失败
  95. $this->debugMsg('未接收到数据');
  96. return [
  97. 'status' => 0,
  98. 'msg' => '未接收到数据',
  99. ];
  100. }
  101. $this->uniacid = request()->input('i');
  102. if (!$this->uniacid) {
  103. $this->debugMsg('未获取到公众号参数');
  104. return [
  105. 'status' => 0,
  106. 'msg' => '未获取到公众号参数',
  107. ];
  108. }
  109. $this->debugMsg('解密前报文', $msg);
  110. \YunShop::app()->uniacid = $this->uniacid;
  111. \Setting::$uniqueAccountId = $this->uniacid;
  112. $silverPointPay = new SilverPointPay();
  113. $reqStr = $silverPointPay->decrypt($msg);
  114. $this->debugMsg('解密后报文', $reqStr);
  115. $responseData = json_decode($reqStr, true);
  116. if (!$responseData) {
  117. $this->debugMsg('解密后报文为空');
  118. return [
  119. 'status' => 0,
  120. 'msg' => '解密后报文为空',
  121. ];
  122. }
  123. $profitShare = ProfitShare::where('profit_share_sn', $responseData['outOrderId'])->first();
  124. if ($responseData['transStatus'] == '100') {
  125. $this->debugMsg('银典数据-分账成功', $responseData);
  126. $profitShare->status = ProfitShare::SPLIT_SUCCESS;
  127. $profitShare->receive_at = time(); // 到账时间
  128. } elseif ($responseData['transStatus'] == '101') {
  129. $this->debugMsg('银典数据-待支付', $responseData);
  130. $profitShare->status = ProfitShare::UN_SPLIT;
  131. } elseif ($responseData['transStatus'] == '102') {
  132. $this->debugMsg('银典数据-支付失败', $responseData);
  133. $profitShare->status = ProfitShare::UN_SPLIT;
  134. } elseif ($responseData['transStatus'] == '103') {
  135. $this->debugMsg('银典数据-订单处理中', $responseData);
  136. $profitShare->status = ProfitShare::SPLITTING;
  137. } elseif ($responseData['transStatus'] == '104') {
  138. $this->debugMsg('银典数据-已撤销', $responseData);
  139. $profitShare->status = ProfitShare::SPLIT_FAIL;
  140. } else {
  141. $profitShare->trans_status = $responseData['transStatus'];
  142. }
  143. $profitShare->save();
  144. return $this->successReply();
  145. }
  146. protected function processOrder($data)
  147. {
  148. $payOrder = PayOrder::where('out_order_no', $data['out_order_no'])->first();
  149. if (!$payOrder) {
  150. $this->debugMsg('未找到支付订单', $data);
  151. return false;
  152. }
  153. $currentPayType = $this->currentPayType($payOrder->type);
  154. $setData = $this->setData(
  155. $payOrder['out_order_no'],
  156. $data['out_order_no'],
  157. bcmul($data['amount'], 100, 0),
  158. $currentPayType['id'],
  159. $currentPayType['name']
  160. );
  161. $this->payResutl($setData);
  162. $this->debugMsg('订单支付成功--订单号', $data['out_order_no']);
  163. return true;
  164. }
  165. protected function getNotifyRequest()
  166. {
  167. $msg = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents(
  168. "php://input"
  169. );
  170. if (empty($msg)) {
  171. # 如果没有数据,直接返回失败
  172. $this->debugMsg('未接收到数据');
  173. return [
  174. 'status' => 0,
  175. 'msg' => '未接收到数据',
  176. ];
  177. }
  178. $this->uniacid = request()->input('i');
  179. if (!$this->uniacid) {
  180. $this->debugMsg('未获取到公众号参数');
  181. return [
  182. 'status' => 0,
  183. 'msg' => '未获取到公众号参数',
  184. ];
  185. }
  186. $this->debugMsg('解密前报文', $msg);
  187. \YunShop::app()->uniacid = $this->uniacid;
  188. \Setting::$uniqueAccountId = $this->uniacid;
  189. $silverPointPay = new SilverPointPay();
  190. $reqStr = $silverPointPay->decrypt($msg);
  191. $this->debugMsg('解密后报文', $reqStr);
  192. $respData = json_decode($reqStr, true);
  193. if ($respData['transStatus'] == '100') {
  194. return [
  195. 'status' => 1,
  196. 'out_order_no' => $respData["outOrderId"], // 外部订单号
  197. 'order_no' => $respData["orderCd"], // 支付订单号
  198. 'amount' => $respData["transAmt"], // 交易金额
  199. 'type_num' => $respData['txnNum'] //"txnNum":"1108"说明是代付订单
  200. ];
  201. } else {
  202. $this->debugMsg('回调失败', $reqStr);
  203. return [
  204. 'status' => 0,
  205. 'msg' => $reqStr,
  206. 'type_num' => $respData['txnNum'] //"txnNum":"1108"说明是代付订单
  207. ];
  208. }
  209. }
  210. protected function successReply()
  211. {
  212. return json_encode([
  213. 'respCode' => '0000',
  214. 'respMsg' => '成功'
  215. ]);
  216. }
  217. protected function debugMsg($title, $data = [])
  218. {
  219. \Log::debug(self::class . ' ' . $title . ': ' . json_encode($data));
  220. }
  221. protected function currentPayType($payId)
  222. {
  223. return PayType::find($payId);
  224. }
  225. /**
  226. * 支付回调参数
  227. *
  228. * @param $order_no
  229. * @param $parameter
  230. * @return array
  231. */
  232. public function setData($order_no, $trade_no, $total_fee, $pay_type_id, $pay_type)
  233. {
  234. return [
  235. 'total_fee' => $total_fee,
  236. 'out_trade_no' => $order_no,
  237. 'trade_no' => $trade_no,
  238. 'unit' => 'fen',
  239. 'pay_type' => $pay_type,
  240. 'pay_type_id' => $pay_type_id,
  241. ];
  242. }
  243. }