OrderController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * OrderController.php UTF-8
  4. * 小号订单
  5. *
  6. * @date : 2018/7/31 10:45
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : Huosdk 8.0
  11. */
  12. namespace api\accountdeal\controller;
  13. use api\common\controller\V2ApiBaseController;
  14. use Exception;
  15. use huo\controller\common\HuoSession;
  16. use huoAccountDeal\controller\AccountOrder;
  17. use huoAccountDeal\model\AccountOrderModel;
  18. use huolib\constant\DeviceTypeConst;
  19. use huolib\constant\WalletConst;
  20. use huolib\pay\Pay;
  21. use huolib\status\AccountStatus;
  22. use huolib\status\OrderStatus;
  23. use huolib\tool\StrUtils;
  24. use think\Lang;
  25. class OrderController extends V2ApiBaseController {
  26. public function _initialize() {
  27. parent::_initialize();
  28. $langSet = $this->request->langset();
  29. Lang::load([APP_PATH.'accountdeal'.DS.'lang'.DS.$langSet.DS.'account'.EXT,]);
  30. }
  31. /**
  32. * 买号-购买小号下单
  33. * http://doc.huosdk.com/138?page_id=3435
  34. * 【域名】/account/goods/buy
  35. */
  36. public function preorder() {
  37. $_param = $this->request->param();
  38. $_result = $this->validate($_param, 'Account.buy');
  39. if ($_result !== true) {
  40. $this->error($_result);
  41. }
  42. $this->checkLogin();
  43. $_mem_id = $this->getMemId();
  44. $_ags_id = $this->request->param('goods_id/d');
  45. $_rdata = (new AccountOrder())->preorder($_mem_id, $_ags_id);
  46. if (AccountStatus::NO_ERROR == $_rdata['code']) {
  47. $_pay_token = md5(md5($_rdata['data']['order_id']).time());
  48. (new HuoSession($this->mem_id))->setAgToken($_pay_token);
  49. $_rdata['data']['token'] = $_pay_token;
  50. }
  51. $this->returnData($_rdata);
  52. }
  53. /**
  54. * 买号-购买小号支付
  55. * http://doc.huosdk.com/138?page_id=3844
  56. * 【域名】/account/goods/pay
  57. */
  58. public function pay() {
  59. $this->checkLogin();
  60. $_check_token = (new HuoSession($this->mem_id))->getAgToken();
  61. $_pay_token = $this->request->param('pay_token/s', '');
  62. if ($_pay_token != $_check_token) {
  63. $_code = AccountStatus::ACCOUNT_TOKEN_ERROR;
  64. $this->error(AccountStatus::getMsg($_code), [], $_code);
  65. }
  66. (new HuoSession($this->mem_id))->clearAgToken();
  67. $_payway = $this->request->param('payway/s', '');
  68. $_order_id = $this->request->param('order_id/s', '');
  69. $_ao_model = (new AccountOrderModel());
  70. $_order_data = $_ao_model->getDetail($_order_id);
  71. if (empty($_order_data)) {
  72. $_code = AccountStatus::ORDER_NOT_EXISTS;
  73. $this->error(AccountStatus::getMsg($_code), [], $_code);
  74. }
  75. $_order_data['payway'] = $_payway;
  76. $_ao_model->updateData($_order_data, $_order_data['id']);
  77. $_product_name = '购买小号';
  78. $_product_desc = $_product_name;
  79. $_pay_rs = null;
  80. try {
  81. $_pay_class = Pay::ins()->get($_payway);
  82. $_func = $this->getFunc($this->device_type);
  83. $_show_url = url('wapapp/'.$_payway.'/show', ['order_id' => $_order_id], false, APISITE);
  84. $_return_url = url(
  85. 'wapapp/'.$_payway.'/return', ['order_id' => $_order_id], false, APISITE
  86. );
  87. $_pay_class->setReturnUrl($_return_url);
  88. $_pay_class->setShowUrl($_show_url);
  89. $_pay_class->setOrderId($_order_id);
  90. $_pay_class->setProductName($_product_name);
  91. $_pay_class->setProductDesc($_product_desc);
  92. $_order_data['real_price'] = StrUtils::formatNumber($_order_data['real_price']);
  93. $_pay_class->setRealAmount($_order_data['real_price']);
  94. $_pay_class->setProductId(WalletConst::WALLET_PRODUCT_ACCOUNT);
  95. $_pay_class->setIp($this->request->ip());
  96. $_pay_class->setOpenId((new HuoSession($this->mem_id))->getOpenId($_order_data['app_id']));
  97. $_pay_rs = $_pay_class->$_func();
  98. } catch (Exception $e) {
  99. $_code = OrderStatus::PAYWAY_NOT_EXISTS;
  100. $this->error(OrderStatus::getMsg($_code), [], $_code);
  101. }
  102. if (empty($_pay_rs)) {
  103. $_code = OrderStatus::PAYWAY_PREORDER_ERROR;
  104. $this->error(OrderStatus::getMsg($_code), [], $_code);
  105. }
  106. $_code = OrderStatus::NO_ERROR;
  107. $this->success(OrderStatus::getMsg($_code), $_pay_rs, $_code);
  108. }
  109. /**
  110. * 买号-获取支付方式
  111. * http://doc.huosdk.com/138?page_id=3549
  112. * 【域名】/account/pay/payway
  113. */
  114. public function getPayways() {
  115. $_rs = (new AccountOrder())->getPayWays();
  116. $this->returnData($_rs);
  117. }
  118. /**
  119. * 买号-取消购买/取消卖出
  120. * http://doc.huosdk.com/138?page_id=3550
  121. * 【域名】/account/order/cancel
  122. */
  123. public function cancel() {
  124. $_param = $this->request->param();
  125. $_result = $this->validate($_param, 'Account.cancelOrder');
  126. if ($_result !== true) {
  127. $this->error($_result);
  128. }
  129. $this->checkLogin();
  130. $_mem_id = $this->getMemId();
  131. $_order_id = $this->request->param('order_id/s', '');
  132. $_is_sell = $this->request->param('is_sell/d', 0);
  133. $_rs = (new AccountOrder())->cancel($_mem_id, $_order_id, $_is_sell);
  134. $this->returnData($_rs);
  135. }
  136. /**
  137. * 玩家-交易订单列表
  138. * http://doc.huosdk.com/138?page_id=3433
  139. * 【域名】/account/order/list
  140. */
  141. public function getList() {
  142. $this->checkLogin();
  143. $_param['buy_mem_id'] = $this->getMemId();
  144. $_status = $this->request->param('status/s', 0);
  145. $_page = $this->request->param('page/d', 1);
  146. $_offset = $this->request->param('offset/d', 10);
  147. $_page = $_page.','.$_offset;
  148. if (!empty($_status)) {
  149. $_param['status'] = $_status;
  150. }
  151. $_rs = (new AccountOrder())->getOrderList($_param, $_page);
  152. $this->returnData($_rs);
  153. }
  154. /**
  155. * @param $device_type
  156. *
  157. * @return string clientPay mobilePay pcPay
  158. */
  159. private function getFunc($device_type) {
  160. switch ($device_type) {
  161. case DeviceTypeConst::DEVICE_TYPE_IOSAPPLESDK:
  162. $_type = 'clientPay';
  163. break;
  164. case DeviceTypeConst::DEVICE_TYPE_IOSSDK:
  165. $_type = 'clientPay';
  166. break;
  167. case DeviceTypeConst::DEVICE_TYPE_IOSAPP:
  168. $_type = 'clientPay';
  169. break;
  170. case DeviceTypeConst::DEVICE_TYPE_IOSH5APP:
  171. $_type = 'clientPay';
  172. break;
  173. case DeviceTypeConst::DEVICE_TYPE_SAFARI:
  174. $_type = 'mobilePay';
  175. break;
  176. case DeviceTypeConst::DEVICE_TYPE_ANDSDK:
  177. $_type = 'clientPay';
  178. break;
  179. case DeviceTypeConst::DEVICE_TYPE_ANDAPP:
  180. $_type = 'clientPay';
  181. break;
  182. case DeviceTypeConst::DEVICE_TYPE_ANDH5APP:
  183. $_type = 'clientPay';
  184. break;
  185. case DeviceTypeConst::DEVICE_TYPE_ANDBROWSER:
  186. $_type = 'mobilePay';
  187. break;
  188. case DeviceTypeConst::DEVICE_TYPE_WEIXIN:
  189. $_type = 'jsPay';
  190. break;
  191. case DeviceTypeConst::DEVICE_TYPE_PC:
  192. $_type = 'clientPay';
  193. break;
  194. case DeviceTypeConst::DEVICE_TYPE_WAP:
  195. $_type = 'mobilePay';
  196. break;
  197. default:
  198. $_type = 'mobilePay';
  199. }
  200. return $_type;
  201. }
  202. /**
  203. * 买号-查询支付结果
  204. * http://doc.huosdk.com/138?page_id=3848
  205. * 【域名】/account/order/query
  206. */
  207. public function orderQuery() {
  208. $this->checkLogin();
  209. $_order_id = $this->request->param('order_id/s', '');
  210. $_rs = (new AccountOrder())->getStatus($_order_id);
  211. if (false == $_rs) {
  212. $this->error(lang('ERROR'));
  213. }
  214. $_rs['order_id'] = $_order_id;
  215. $this->success(lang('SUCCESS'), $_rs);
  216. }
  217. }