Pay2.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 Pay2 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. $pro_info = json_decode($order['pro_info'],true);
  80. for ($i=0;$i<$order['num'];$i++){
  81. //获取排名
  82. $rank = getRanking($order['c_id']);
  83. $tag = getTag($order['c_id'],$rank,$order['inventory']);
  84. saveRanking($order['c_id']);
  85. $company = '象寻数字科技(上海)有限公司';
  86. $hash = getCompanyHash($order['c_id']);
  87. $company_hash = $hash['hash'];
  88. $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid');
  89. $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s');
  90. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  91. $collectors_hash = '';
  92. $date = [
  93. 'order_id'=>$order['id'],
  94. 'order_no'=>get_order_sn(),
  95. 'tag'=>$tag,
  96. 'mid'=>$order['mid'],
  97. 'c_id'=>$order['c_id'],
  98. 'name'=>$pro_info['name'],
  99. 'cover'=>$pro_info['cover'],
  100. 'pro_info'=>$order['pro_info'],
  101. 'company'=>$company,
  102. 'company_hash'=>$company_hash,
  103. 'company_hash_time'=>$company_hash_time,
  104. 'ddcid'=>$ddcid,
  105. 'collectors_hash'=>$collectors_hash,
  106. 'collectors_hash_time'=>''
  107. ];
  108. $array[] = $date;
  109. }
  110. Db::name('store_order_info')->insertAll($array);
  111. //送积分
  112. $by_collection_integral = getConfigValue('by_collection_integral');
  113. if ($by_collection_integral){
  114. $by_collection_integral = bcmul($by_collection_integral,$order['num']);
  115. memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']);
  116. }
  117. Db::commit();
  118. return true;
  119. } catch (\Exception $e){
  120. Db::rollback();
  121. return false;
  122. }
  123. }
  124. /**
  125. * 微信支付--充值盲盒成功回调订单
  126. */
  127. public function BlindRechargeNotify(){
  128. $payXml = file_get_contents("php://input");
  129. //将xml格式转化为json格式
  130. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  131. //将json格式转成数组格式 $result['out_trade_no']
  132. $result = json_decode($jsonXml, true);
  133. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  134. $return = $this->dealBox($result);
  135. if ($return){
  136. $arr = array(
  137. 'return_code' => 'SUCCESS',
  138. 'return_msg' => 'OK',
  139. );
  140. return $this->arrayToXml($arr);
  141. }else{
  142. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  143. }
  144. }
  145. }
  146. /**
  147. * 支付宝支付--充值盲盒回调订单
  148. */
  149. public function alipayBlindRechargeNotify(){
  150. $result = input('post.');
  151. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  152. $return = $this->dealBox($result);
  153. if ($return){
  154. echo 'success';
  155. }else{
  156. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  157. }
  158. }
  159. }
  160. /**
  161. * 盲盒充值次数处理数据库信息
  162. * @param $result
  163. * @return bool
  164. */
  165. function dealBox($result){
  166. Db::startTrans();
  167. try {
  168. $order = Db::name('store_blind_recharge')->where('order_no',$result['out_trade_no'])->find();
  169. if ($order['status']==1) return true;
  170. Db::name('store_blind_recharge')
  171. ->where('order_no',$result['out_trade_no'])
  172. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  173. //用户加次数
  174. Db::name('store_member')->where('id',$order['m_id'])->setInc('lottery_number',$order['num']);
  175. Db::commit();
  176. return true;
  177. } catch (\Exception $e){
  178. Db::rollback();
  179. return false;
  180. }
  181. }
  182. /**
  183. * 数组转xml
  184. * @ApiInternal
  185. */
  186. public function arrayToXml($arr)
  187. {
  188. $xml = "<xml>";
  189. foreach ($arr as $key => $val) {
  190. if (is_numeric($val)) {
  191. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  192. } else
  193. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  194. }
  195. $xml .= "</xml>";
  196. return $xml;
  197. }
  198. }