Pay.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use EasyWeChat\Factory;
  16. use think\Controller;
  17. use think\Db;
  18. use think\Exception;
  19. use AlibabaCloud\Client\AlibabaCloud;
  20. use AlibabaCloud\Client\Exception\ClientException;
  21. use AlibabaCloud\Client\Exception\ServerException;
  22. use function AlibabaCloud\Client\value;
  23. /**
  24. * 支付管理类
  25. * Class Refund
  26. * @package app\api\controller\Refund
  27. */
  28. class Pay extends Controller
  29. {
  30. /**
  31. * 微信支付--商品支付成功回调订单
  32. */
  33. public function WxOrderNotify(){
  34. $payXml = file_get_contents("php://input");
  35. //将xml格式转化为json格式
  36. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  37. //将json格式转成数组格式 $result['out_trade_no']
  38. $result = json_decode($jsonXml, true);
  39. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  40. $return = $this->dealData($result);
  41. if ($return){
  42. $arr = array(
  43. 'return_code' => 'SUCCESS',
  44. 'return_msg' => 'OK',
  45. );
  46. return $this->arrayToXml($arr);
  47. }else{
  48. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  49. }
  50. }
  51. }
  52. /**
  53. * 支付宝支付--支付成功回调订单
  54. */
  55. public function alipayOrderNotify(){
  56. $result = input('post.');
  57. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  58. $return = $this->dealData($result);
  59. if ($return){
  60. echo 'success';
  61. }else{
  62. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  63. }
  64. }
  65. }
  66. /**
  67. * 处理数据库信息
  68. * @param $result
  69. * @return bool
  70. */
  71. function dealData($result){
  72. Db::startTrans();
  73. try {
  74. $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find();
  75. Db::name('store_order')
  76. ->where('order_no',$result['out_trade_no'])
  77. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  78. $array = [];
  79. for ($i=0;$i<$order['num'];$i++){
  80. //获取排名
  81. $rank = getRanking($order['c_id'])+1;
  82. $tag = getTag($order['c_id'],$rank,$order['inventory']);
  83. saveRanking($order['c_id']);
  84. $company = '象寻数字科技(上海)有限公司';
  85. $hash = getCompanyHash($order['c_id']);
  86. $company_hash = $hash['hash'];
  87. $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid');
  88. $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s');
  89. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  90. $collectors_hash = '';
  91. $date = [
  92. 'order_id'=>$order['id'],
  93. 'order_no'=>get_order_sn(),
  94. 'tag'=>$tag,
  95. 'mid'=>$order['mid'],
  96. 'c_id'=>$order['c_id'],
  97. 'pro_info'=>$order['pro_info'],
  98. 'company'=>$company,
  99. 'company_hash'=>$company_hash,
  100. 'company_hash_time'=>$company_hash_time,
  101. 'ddcid'=>$ddcid,
  102. 'collectors_hash'=>$collectors_hash,
  103. 'collectors_hash_time'=>''
  104. ];
  105. $array[] = $date;
  106. }
  107. Db::name('store_order_info')->insertAll($array);
  108. //送积分
  109. $by_collection_integral = getConfigValue('by_collection_integral');
  110. if ($by_collection_integral){
  111. $by_collection_integral = bcmul($by_collection_integral,$order['num']);
  112. memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']);
  113. }
  114. Db::commit();
  115. return true;
  116. } catch (\Exception $e){
  117. Db::rollback();
  118. return false;
  119. }
  120. }
  121. /**
  122. * 微信支付--充值盲盒成功回调订单
  123. */
  124. public function BlindRechargeNotify(){
  125. $payXml = file_get_contents("php://input");
  126. //将xml格式转化为json格式
  127. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  128. //将json格式转成数组格式 $result['out_trade_no']
  129. $result = json_decode($jsonXml, true);
  130. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  131. $return = $this->dealBox($result);
  132. if ($return){
  133. $arr = array(
  134. 'return_code' => 'SUCCESS',
  135. 'return_msg' => 'OK',
  136. );
  137. return $this->arrayToXml($arr);
  138. }else{
  139. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  140. }
  141. }
  142. }
  143. /**
  144. * 支付宝支付--充值盲盒回调订单
  145. */
  146. public function alipayBlindRechargeNotify(){
  147. $result = input('post.');
  148. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  149. $return = $this->dealBox($result);
  150. if ($return){
  151. echo 'success';
  152. }else{
  153. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  154. }
  155. }
  156. }
  157. /**
  158. * 盲盒充值次数处理数据库信息
  159. * @param $result
  160. * @return bool
  161. */
  162. function dealBox($result){
  163. Db::startTrans();
  164. try {
  165. $order = Db::name('store_blind_recharge')->where('order_no',$result['out_trade_no'])->find();
  166. if ($order['status']==1) return true;
  167. Db::name('store_blind_recharge')
  168. ->where('order_no',$result['out_trade_no'])
  169. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  170. //用户加次数
  171. Db::name('store_member')->where('id',$order['m_id'])->setInc('lottery_number',$order['num']);
  172. Db::commit();
  173. return true;
  174. } catch (\Exception $e){
  175. Db::rollback();
  176. return false;
  177. }
  178. }
  179. /**
  180. * 数组转xml
  181. * @ApiInternal
  182. */
  183. public function arrayToXml($arr)
  184. {
  185. $xml = "<xml>";
  186. foreach ($arr as $key => $val) {
  187. if (is_numeric($val)) {
  188. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  189. } else
  190. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  191. }
  192. $xml .= "</xml>";
  193. return $xml;
  194. }
  195. }