ClientpayController.php 6.7 KB

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