ClientpayController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\HuoSession;
  15. use huo\controller\pay\GamePay;
  16. use huo\controller\pay\PtbPay;
  17. use huo\controller\pay\SdkOrderCache;
  18. use huo\controller\pay\SdkPayCache;
  19. use huolib\constant\CommonConst;
  20. use huolib\constant\DeviceTypeConst;
  21. use huolib\constant\GameConst;
  22. use huolib\constant\OrderConst;
  23. use huolib\constant\PaywayConst;
  24. use huolib\pay\Pay;
  25. use huolib\status\OrderStatus;
  26. use huolib\utils\OrderUtils;
  27. class ClientpayController extends V2ApiBaseController {
  28. function _initialize() {
  29. parent::_initialize();
  30. }
  31. /**
  32. * 选择支付页面支付
  33. * http://doc.1tsdk.com/138?page_id=3151
  34. *【域名】/wap/pay/post
  35. *
  36. */
  37. public function pay() {
  38. $this->checkLogin();
  39. $_order_id = get_val($this->rq_data, 'order_id');
  40. $_client = get_val($this->rq_data, 'client');
  41. $_game_rq = $this->setGameData();
  42. if (!empty($_order_id)) {
  43. $this->rq_data['order']['order_id'] = $_order_id;
  44. }
  45. $_order_rq = $this->setOrderData();
  46. $_device_rq = $this->setDeviceData();
  47. $_pay_token = get_val($this->rq_data, 'pay_token', '');
  48. $_rs = $_order_rq->checkPayToken($_pay_token);
  49. if (false == $_rs) {
  50. $_code = OrderStatus::TOKEN_ARGS_ERROR;
  51. $this->error(OrderStatus::getMsg($_code), [], $_code);
  52. }
  53. $_payway = get_val($this->rq_data, 'payway', '');
  54. $_pw_check_rs = OrderUtils::checkPayway($_payway);
  55. if (OrderStatus::NO_ERROR != $_pw_check_rs) {
  56. $this->error(OrderStatus::getMsg($_pw_check_rs), [], $_pw_check_rs);
  57. }
  58. /* 判断是否使用平台币 */
  59. $_has_ptb = get_val($this->rq_data, 'has_ptb', 2);
  60. if (OrderConst::PAY_HAS_PTB == $_has_ptb) {
  61. /* 使用平台币 先记录入缓存 */
  62. $_rdata = (new PtbPay())->PtbOrder($_order_rq->getOrderId());
  63. if (OrderStatus::NO_ERROR != $_rdata['code']) {
  64. $this->returnData($_rdata);
  65. }
  66. }
  67. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($_order_rq->getOrderId());
  68. if ($_order_data['real_amount'] <= CommonConst::CONST_ZERO_COMPARE) {
  69. /* 完全使用平台币支付 */
  70. $_rs = (new PtbPay())->ptbSdkPay($_order_rq->getOrderId());
  71. $this->returnData($_rs);
  72. }
  73. /* 游戏币消费 特有流程 */
  74. if (PaywayConst::PAYWAY_GAMEPAY == strtolower($_payway)) {
  75. $_rs = (new GamePay())->GameSdkPay($_order_rq->getOrderId());
  76. $this->returnData($_rs);
  77. }
  78. if (empty($_order_data)) {
  79. $_code = OrderStatus::ORDER_NOT_EXISTS;
  80. $this->error(OrderStatus::getMsg($_code), [], $_code);
  81. }
  82. $_order_rq->setDataFromOrder($_order_data);
  83. $_pay_class = Pay::ins()->get($_payway);
  84. $_func = $this->getFunc($this->device_type);
  85. /*微信支付 走 mobilepay*/
  86. if (PaywayConst::PAYWAY_WXPAY == $_payway) {
  87. $_func = 'mobilePay';
  88. }
  89. $_show_url = url(
  90. 'sdk/'.$_payway.'/showUrl', ['order_id' => $_order_rq->getOrderId(), 'apple_id' => $_game_rq->getAppleId()],
  91. false, APISITE
  92. );
  93. $_return_url = url(
  94. 'sdk/'.$_payway.'/returnUrl',
  95. ['order_id' => $_order_rq->getOrderId(), 'apple_id' => $_game_rq->getAppleId()], false, APISITE
  96. );
  97. $_notify_url = url('sdk/'.$_payway.'/notifyUrl', '', false, APISITE);
  98. $_pay_class->setNotifyUrl($_notify_url);
  99. $_pay_class->setReturnUrl($_return_url);
  100. $_pay_class->setShowUrl($_show_url);
  101. $_pay_class->setOrderId($_order_rq->getOrderId());
  102. $_pay_class->setProductName($_order_rq->getProductName());
  103. $_pay_class->setProductDesc($_order_rq->getProductDesc());
  104. $_pay_class->setRealAmount($_order_rq->getProductRealPrice());
  105. $_pay_class->setProductPrice($_order_rq->getProductPrice());
  106. $_pay_class->setProductId($_order_rq->getProductId());
  107. $_pay_class->setIp($this->request->ip());
  108. $_pay_class->setOpenId((new HuoSession($this->mem_id, $_game_rq->getHAppId()))->getOpenId());
  109. if (GameConst::GAME_IOS_SWITCH == $_device_rq->getFrom()) {
  110. $_pay_rs = $_pay_class->mobilePay();
  111. /*
  112. $_pay_data['token'] = $_pay_rs['token'];
  113. $_pay_data['pay_token'] = $_order_rq->genPayToken($_order_id);
  114. SdkPayCache::ins()->saveOrderCache($_order_rq->getOrderId(), $_pay_data); //存缓存
  115. $_pay_rs['token'] = url(
  116. 'sdk/'.$_payway.'/submitUrl',
  117. ['order_id' => $_order_rq->getOrderId(), 'pay_token' => $_pay_data['pay_token']], false, APISITE
  118. );*/
  119. } else {
  120. $_pay_rs = $_pay_class->$_func();
  121. }
  122. if (empty($_pay_rs)) {
  123. $_code = OrderStatus::PAYWAY_PREORDER_ERROR;
  124. $this->error(OrderStatus::getMsg($_code), [], $_code);
  125. }
  126. /* 更新支付方式 */
  127. $_pay_alias = $_pay_rs['pay_type'];
  128. $_order_data['payway'] = $_pay_alias;
  129. SdkOrderCache::ins()->updateOrder($_order_rq->getOrderId(), $_order_data);
  130. if (PaywayConst::PAYWAY_WXPAY == $_payway) {
  131. $_pay_data = [];
  132. $_pay_data['token'] = $_pay_rs['token'];
  133. SdkPayCache::ins()->saveOrderCache($_order_rq->getOrderId(), $_pay_data); //存缓存
  134. $_pay_rs['token'] = url(
  135. 'sdk/'.$_payway.'/payUrl', ['order_id' => $_order_rq->getOrderId()], false, APISITE
  136. );
  137. }
  138. $_code = OrderStatus::NO_ERROR;
  139. $this->success(OrderStatus::getMsg($_code), $_pay_rs, $_code);
  140. }
  141. /**
  142. * @param $device_type
  143. *
  144. * @return string clientPay mobilePay pcPay
  145. */
  146. public function getFunc($device_type) {
  147. switch ($device_type) {
  148. case DeviceTypeConst::DEVICE_TYPE_IOSAPPLESDK:
  149. $_type = 'clientPay';
  150. break;
  151. case DeviceTypeConst::DEVICE_TYPE_IOSSDK:
  152. $_type = 'clientPay';
  153. break;
  154. case DeviceTypeConst::DEVICE_TYPE_IOSAPP:
  155. $_type = 'clientPay';
  156. break;
  157. case DeviceTypeConst::DEVICE_TYPE_IOSH5APP:
  158. $_type = 'clientPay';
  159. break;
  160. case DeviceTypeConst::DEVICE_TYPE_SAFARI:
  161. $_type = 'mobilePay';
  162. break;
  163. case DeviceTypeConst::DEVICE_TYPE_ANDSDK:
  164. $_type = 'clientPay';
  165. break;
  166. case DeviceTypeConst::DEVICE_TYPE_ANDAPP:
  167. $_type = 'clientPay';
  168. break;
  169. case DeviceTypeConst::DEVICE_TYPE_ANDH5APP:
  170. $_type = 'clientPay';
  171. break;
  172. case DeviceTypeConst::DEVICE_TYPE_ANDBROWSER:
  173. $_type = 'mobilePay';
  174. break;
  175. case DeviceTypeConst::DEVICE_TYPE_WEIXIN:
  176. $_type = 'jsPay';
  177. break;
  178. case DeviceTypeConst::DEVICE_TYPE_PC:
  179. $_type = 'pcPay';
  180. break;
  181. case DeviceTypeConst::DEVICE_TYPE_WAP:
  182. $_type = 'mobilePay';
  183. break;
  184. default:
  185. $_type = 'mobilePay';
  186. }
  187. return $_type;
  188. }
  189. }