Infopay.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 支付接口
  7. */
  8. namespace app\citys\controller\api\v1;
  9. use app\citys\controller\api\Base;
  10. use app\citys\model\Citys;
  11. use app\citys\model\CitysOrder;
  12. use app\citys\model\CitysConfig;
  13. use app\citys\model\CitysPhone;
  14. use app\common\facade\WechatPay;
  15. use filter\Filter;
  16. class Infopay extends Base{
  17. /**
  18. * 初始化当前应用是否登录
  19. * @return void
  20. */
  21. public function initialize() {
  22. parent::initialize();
  23. $this->isUserAuth();
  24. }
  25. /**
  26. * 购买商品
  27. */
  28. public function index(){
  29. if (request()->isPost()) {
  30. $param['id'] = $this->request->param('id/d');
  31. $param['telphone'] = $this->request->param('telphone/s','');
  32. $param['message'] = $this->request->param('message','');
  33. $param['ucode'] = $this->request->param('ucode','');
  34. $param['fields'] = $this->request->param('fields/s','[]','htmlspecialchars_decode');
  35. $this->apiSign($param);
  36. $param['fields'] = json_encode(Filter::filter_escape(json_decode($param['fields'],true)));
  37. //判断是否已下架
  38. $item = Citys::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_lock' => 0])->find();
  39. if(empty($item)){
  40. return enjson(403,'信息不存在');
  41. }
  42. //读取订单
  43. $order_no = order_no($this->user->invite_code);
  44. $amount = $item->price;
  45. //唤醒微信支付参数
  46. $payparm = [
  47. 'openid' => $this->user->miniapp_uid,
  48. 'miniapp_id' => $this->miniapp_id,
  49. 'name' => $item->mp->title,
  50. 'order_no' => $order_no,
  51. 'total_fee' => $amount <= 0 ? 1 : $amount * 100,
  52. 'notify_url' => api(1,'ais/notify/infoOrder',$this->miniapp_id),
  53. ];
  54. //读取配置
  55. if($this->member_miniapp->is_psp){
  56. $setting = CitysConfig::getConfig($this->miniapp_id);
  57. if (empty($setting->mchid)) {
  58. return enjson(403,'服务商模式下,必须配置默认商户号');
  59. }
  60. $payparm['mchid'] = $setting->mchid;
  61. }
  62. $ispay = WechatPay::orderPay($payparm);
  63. if ($ispay['code'] == 0) {
  64. return enjson(403,$ispay['msg']);
  65. }
  66. //判断是否新订单
  67. $param['member_miniapp_id'] = $this->miniapp_id;
  68. $param['uid'] = $this->user->id;
  69. $param['info_uid'] = $item->uid;
  70. $param['info_id'] = $item->id;
  71. $param['phone'] = $param['telphone'];
  72. $param['amount'] = $amount;
  73. $param['order_no'] = $order_no;
  74. $param['message'] = $param['message'];
  75. $param['fields'] = $param['fields'];
  76. $param['cache'] = $item->toJson();
  77. $rel = CitysOrder::insertOrder($param,$order_no);
  78. if(empty($rel)){
  79. return enjson(204,'购买商品失败');
  80. }
  81. return enjson(200,'成功',$ispay['data']);
  82. }
  83. }
  84. /**
  85. * 我的订单
  86. * @return void
  87. */
  88. public function order(){
  89. $param['active'] = $this->request->param('active/d',0);
  90. $param['page'] = $this->request->param('page/d',1);
  91. $this->apiSign($param);
  92. $condition['uid'] = $this->user->id;
  93. $condition['is_del'] = 0;
  94. switch ($param['active']) {
  95. case 1:
  96. $condition['paid_at'] = 0;
  97. break;
  98. case 2:
  99. $condition['paid_at'] = 1;
  100. $condition['status'] = 0;
  101. break;
  102. case 3:
  103. $condition['paid_at'] = 1;
  104. $condition['status'] = 1;
  105. break;
  106. }
  107. $lists = CitysInfoOrder::where($condition)->field('cache',true)->order('id desc')->paginate(10)->toArray();
  108. $data = [];
  109. foreach ($lists['data'] as $key => $value) {
  110. $data[$key] = $value;
  111. $data[$key]['fields'] = json_decode($value['fields'],true);
  112. }
  113. return enjson(200,'成功',$data);
  114. }
  115. /**
  116. * 显示号码
  117. * @return void
  118. */
  119. public function showPhone(){
  120. if (request()->isPost()) {
  121. $param['id'] = $this->request->param('id/d');
  122. $this->apiSign($param);
  123. //判断是否已下架
  124. $item = Citys::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_lock' => 0])->find();
  125. if(empty($item)){
  126. return enjson(403,'信息不存在');
  127. }
  128. $price = ($item->types->price??0)+($item->cate->price??0);
  129. //判断是否重复购买
  130. $phone = CitysPhone::where(['info_id' => $item->id,'uid'=>$this->user->id])->find();
  131. if(!empty($phone) && $phone->paid_at == 1){
  132. return enjson(403,'不用重复购买');
  133. }
  134. $setting = CitysConfig::config($this->miniapp_id);
  135. //读取订单
  136. $order_no = order_no($this->user->invite_code);
  137. if($phone){
  138. $phone->price = $price;
  139. $phone->order_no = $order_no;
  140. $rel = $phone->save();
  141. }else{
  142. //判断是否新订单
  143. $data['member_miniapp_id'] = $this->miniapp_id;
  144. $data['uid'] = $this->user->id;
  145. $data['info_id'] = $item->id;
  146. $data['price'] = $price;
  147. $data['order_no'] = $order_no;
  148. $rel = CitysPhone::create($data);
  149. }
  150. if(empty($rel)){
  151. return enjson(204,'下单失败失败');
  152. }
  153. //吊起支付
  154. $payparm = [
  155. 'openid' => $this->user->miniapp_uid,
  156. 'miniapp_id' => $this->miniapp_id,
  157. 'name' => '电话服务',
  158. 'order_no' => $order_no,
  159. 'total_fee' => $price*100,
  160. 'notify_url' => api(1,'citys/notify/showPhone',$this->miniapp_id),
  161. ];
  162. //读取配置
  163. if($this->member_miniapp->is_psp){
  164. if (empty($setting->mch_id)) {
  165. return enjson(403,'服务商模式下,必须配置默认商户号');
  166. }
  167. $payparm['mchid'] = $setting->mch_id;
  168. }
  169. $ispay = WechatPay::orderPay($payparm);
  170. if ($ispay['code'] == 0) {
  171. return enjson(403,$ispay['msg']);
  172. }
  173. return enjson(200,'成功',$ispay['data']);
  174. }
  175. }
  176. }