WappayController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * WappayController.php UTF-8
  4. *
  5. *
  6. * @date : 2018/3/17 16:26
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace api\sdk\controller;
  13. use api\common\controller\V2ApiBaseController;
  14. use huo\controller\common\HuoCookie;
  15. use huo\controller\common\HuoSession;
  16. use huo\controller\conf\PaywayConf;
  17. use huo\controller\member\MemCache;
  18. use huo\controller\pay\PtbPay;
  19. use huo\controller\pay\SdkOrderCache;
  20. use huolib\constant\DeviceTypeConst;
  21. use huolib\constant\PaywayConst;
  22. use huolib\pay\Pay;
  23. use huolib\status\OrderStatus;
  24. use huolib\utils\OrderUtils;
  25. class WappayController extends V2ApiBaseController {
  26. function _initialize() {
  27. parent::_initialize();
  28. }
  29. /**
  30. * 选择支付页面支付
  31. * http://doc.1tsdk.com/138?page_id=3151
  32. *【域名】/wap/pay/post
  33. *
  34. */
  35. public function pay() {
  36. $this->checkLogin();
  37. $_mc_class = MemCache::ins();
  38. $_mem_data = $_mc_class->getInfoById($this->mem_id);
  39. $_order_id = get_val($this->rq_data, 'order_id', '');
  40. if (!empty($_order_id)) {
  41. $this->rq_data['order']['order_id'] = $_order_id;
  42. }
  43. $_order_rq = $this->setOrderData();
  44. $_game_rq = $this->setGameData();
  45. $_pay_token = get_val($this->rq_data, 'pay_token', '');
  46. $_rs = $_order_rq->checkPayToken($_pay_token);
  47. if (false == $_rs) {
  48. $_code = OrderStatus::TOKEN_ARGS_ERROR;
  49. // $this->error(OrderStatus::getMsg($_code), [], $_code);
  50. }
  51. $_payway = get_val($this->rq_data, 'payway', '');
  52. $_pw_check_rs = OrderUtils::checkPayway($_payway);
  53. if (OrderStatus::NO_ERROR != $_pw_check_rs) {
  54. $this->error(OrderStatus::getMsg($_pw_check_rs), [], $_pw_check_rs);
  55. }
  56. /* 平台币 游戏币消费 特有流程 */
  57. if (PaywayConst::PAYWAY_PTBPAY == strtolower($_payway)) {
  58. $_rs = (new PtbPay())->ptbSdkPay($_order_rq->getOrderId());
  59. $this->returnData($_rs);
  60. }
  61. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($_order_rq->getOrderId());
  62. if (empty($_order_data)) {
  63. $_code = OrderStatus::ORDER_NOT_EXISTS;
  64. $this->error(OrderStatus::getMsg($_code), [], $_code);
  65. }
  66. $_order_rq->setDataFromOrder($_order_data);
  67. $_pay_class = Pay::ins()->get($_payway);
  68. $_func = $this->getFunc($this->device_type);
  69. $_show_url = url('sdk/'.$_payway.'/showUrl', ['order_id' => $_order_rq->getOrderId()], false, APISITE);
  70. $_return_url = url(
  71. 'sdk/'.$_payway.'/returnUrl', ['order_id' => $_order_rq->getOrderId()], false, APISITE
  72. );
  73. $_notify_url = url('sdk/'.$_payway.'/notifyUrl', '', false, APISITE);
  74. $_pay_class->setNotifyUrl($_notify_url);
  75. $_pay_class->setReturnUrl($_return_url);
  76. $_pay_class->setShowUrl($_show_url);
  77. $_pay_class->setOrderId($_order_rq->getOrderId());
  78. $_pay_class->setProductName($_order_rq->getProductName());
  79. $_pay_class->setProductDesc($_order_rq->getProductDesc());
  80. $_pay_class->setRealAmount($_order_rq->getProductRealPrice());
  81. $_pay_class->setProductPrice($_order_rq->getProductPrice());
  82. $_pay_class->setProductId($_order_rq->getProductId());
  83. $_pay_class->setIp($this->request->ip());
  84. // if (!empty($_game_rq->getVbId())) {
  85. // $_pay_class->setUseProtocol(true);
  86. // }
  87. // 都走协议
  88. $_pay_class->setUseProtocol(true);
  89. $_huo_session = new HuoSession($this->mem_id);
  90. $_huo_session->setType($this->getDeviceType());
  91. $_open_id = $_huo_session->getOpenId();
  92. if (empty($_open_id)) {
  93. $_cookie_open_id = HuoCookie::getOpenId();
  94. if (!empty($_cookie_open_id)) {
  95. $_open_id = $_cookie_open_id;
  96. }
  97. }
  98. $_pay_class->setOpenId($_open_id);
  99. $_pay_class->setMemId($this->mem_id);
  100. $_pay_class->setMemRealName($_mem_data['real_name']);
  101. $_pay_class->setMemRegTime($_mem_data['create_time']);
  102. $_pay_class->setMemMobile($_mem_data['mobile']);
  103. $_config = (new PaywayConf())->getConfByAppPayway($_order_rq->getAppId(), $_payway);
  104. $_pay_alias = empty($_config['payname']) ? $_payway : $_config['payname'];
  105. $_pay_rs = $_pay_class->$_func($_config);
  106. if (empty($_pay_rs)) {
  107. $_code = OrderStatus::PAYWAY_PREORDER_ERROR;
  108. $this->error(OrderStatus::getMsg($_code), [], $_code);
  109. }
  110. /* 更新支付方式 */
  111. // $_pay_alias = $_pay_rs['pay_type'];
  112. $_order_data['payway'] = $_pay_alias;
  113. SdkOrderCache::ins()->updateOrder($_order_rq->getOrderId(), $_order_data);
  114. $_code = OrderStatus::NO_ERROR;
  115. $this->success(OrderStatus::getMsg($_code), $_pay_rs, $_code);
  116. }
  117. /**
  118. * @param $device_type
  119. *
  120. * @return string clientPay mobilePay pcPay
  121. */
  122. public function getFunc($device_type) {
  123. switch ($device_type) {
  124. case DeviceTypeConst::DEVICE_TYPE_IOSAPPLESDK:
  125. $_type = 'clientPay';
  126. break;
  127. case DeviceTypeConst::DEVICE_TYPE_IOSSDK:
  128. $_type = 'clientPay';
  129. break;
  130. case DeviceTypeConst::DEVICE_TYPE_IOSAPP:
  131. $_type = 'clientPay';
  132. break;
  133. case DeviceTypeConst::DEVICE_TYPE_IOSH5APP:
  134. $_type = 'clientPay';
  135. break;
  136. case DeviceTypeConst::DEVICE_TYPE_SAFARI:
  137. $_type = 'mobilePay';
  138. break;
  139. case DeviceTypeConst::DEVICE_TYPE_ANDSDK:
  140. $_type = 'clientPay';
  141. break;
  142. case DeviceTypeConst::DEVICE_TYPE_ANDAPP:
  143. $_type = 'clientPay';
  144. break;
  145. case DeviceTypeConst::DEVICE_TYPE_ANDH5APP:
  146. $_type = 'clientPay';
  147. break;
  148. case DeviceTypeConst::DEVICE_TYPE_ANDBROWSER:
  149. $_type = 'mobilePay';
  150. break;
  151. case DeviceTypeConst::DEVICE_TYPE_WEIXIN:
  152. $_type = 'jsPay';
  153. break;
  154. case DeviceTypeConst::DEVICE_TYPE_PC:
  155. $_type = 'pcPay';
  156. break;
  157. case DeviceTypeConst::DEVICE_TYPE_WAP:
  158. $_type = 'mobilePay';
  159. break;
  160. default:
  161. $_type = 'mobilePay';
  162. }
  163. return $_type;
  164. }
  165. }