No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

273 líneas
8.9 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/1/7
  6. * Time: 10:01 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\epay\library\Service;
  10. use addons\unishop\extend\Ali;
  11. use addons\unishop\extend\Hashids;
  12. use addons\unishop\extend\Wechat;
  13. use addons\unishop\model\Config;
  14. use think\Db;
  15. use think\Exception;
  16. use think\Hook;
  17. use think\Log;
  18. class Pay extends Base
  19. {
  20. protected $noNeedLogin = ['getPayType', 'notify', 'authRedirect', 'alipay', 'alinotify'];
  21. /**
  22. * 获取支付类型
  23. */
  24. public function getPayType()
  25. {
  26. $platfrom = $this->request->header('platform');
  27. $type = [];
  28. $offline = Config::getByName('offline_pay')['value'] == 1 ? true : false;
  29. switch ($platfrom) {
  30. case 'APP-PLUS';
  31. $type = ['alipay' => true, 'wxpay' => true, 'offline' => $offline];
  32. break;
  33. case 'H5':
  34. $type = ['alipay' => true, 'wxpay' => true, 'offline' => $offline];
  35. // 如果是微信内访问 公众号等
  36. if (Wechat::h5InWechat()) {
  37. $type['alipay'] = false;
  38. }
  39. break;
  40. case 'MP-WEIXIN':
  41. $type = ['alipay' => false, 'wxpay' => true, 'offline' => $offline];
  42. break;
  43. case 'MP-ALIPAY':
  44. $type = ['alipay' => true, 'wxpay' => false, 'offline' => $offline];
  45. break;
  46. case 'MP-BAIDU':
  47. $type = ['alipay' => false, 'wxpay' => false, 'offline' => $offline];
  48. break;
  49. case 'MP-TOUTIAO':
  50. $type = ['alipay' => false, 'wxpay' => false, 'offline' => $offline];
  51. break;
  52. }
  53. $this->success('', $type);
  54. }
  55. /**
  56. * 微信统一下单接口
  57. */
  58. public function unify()
  59. {
  60. $orderId = $this->request->request('order_id', 0);
  61. $orderId = Hashids::decodeHex($orderId);
  62. $orderModel = new \addons\unishop\model\Order();
  63. $order = $orderModel->where(['id' => $orderId])->find();
  64. try {
  65. if (!$order) {
  66. $this->error(__('Order does not exist'));
  67. }
  68. //MWEB
  69. $platfrom = $this->request->header('platform', 'MP-WEIXIN');
  70. switch ($platfrom) {
  71. case 'MP-WEIXIN':
  72. $trade_type = 'JSAPI';
  73. break;
  74. case 'H5':
  75. case 'APP-PLUS':
  76. $trade_type = 'MWEB';
  77. break;
  78. }
  79. // 如果是微信内访问 公众号等
  80. if (Wechat::h5InWechat()) {
  81. $trade_type = 'JSAPI';
  82. }
  83. $products = $order->products()->select();
  84. $body = Config::getByName('name')['value'];
  85. foreach ($products as $product) {
  86. $body .= '_' . $product['title'];
  87. }
  88. $app = Wechat::initEasyWechat('payment');
  89. $result = $app->order->unify([
  90. 'body' => $body,
  91. 'out_trade_no' => $order['out_trade_no'],
  92. 'total_fee' => bcmul($order['total_price'], 100),
  93. 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
  94. 'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
  95. 'openid' => Wechat::getOpenidByUserId($this->auth->id)
  96. ]);
  97. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  98. if ($trade_type == 'JSAPI') {
  99. // 二次签名
  100. $result['timeStamp'] = (string)time();
  101. $result['paySign'] = Wechat::paySign([
  102. 'appId' => Config::getByName('app_id')['value'],
  103. 'nonceStr' => $result['nonce_str'],
  104. 'package' => 'prepay_id=' . $result['prepay_id'],
  105. 'timeStamp' => $result['timeStamp'],
  106. 'signType' => 'MD5'
  107. ], Config::getByName('key')['value']);
  108. } elseif ($trade_type == 'MWEB') {
  109. $page = '/pages/order/order?state=0';
  110. if ($platfrom == 'APP-PLUS') {
  111. $page = '/pages/index/index';
  112. }
  113. $result['mweb_url'] .= '&redirect_url=' . urlencode('https://' . $_SERVER['HTTP_HOST'] . '/h5/#' . $page);
  114. $result['referer'] = 'https://' . $_SERVER['HTTP_HOST'];
  115. }
  116. $this->success('', $result);
  117. } else {
  118. $this->error($result['return_msg']);
  119. }
  120. } catch (Exception $e) {
  121. $this->error($e->getMessage());
  122. }
  123. }
  124. /**
  125. * 支付通知回调
  126. */
  127. public function aliNotify()
  128. {
  129. // 添加行为
  130. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  131. Hook::add('paid_fail', 'addons\\unishop\\behavior\\Order');
  132. $paytype = "alipay";
  133. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/".date("d"). '.log', $paytype . PHP_EOL, FILE_APPEND);
  134. $pay = Service::checkNotify($paytype);
  135. if (!$pay) {
  136. echo '签名错误';
  137. return;
  138. }
  139. $data = $pay->verify();
  140. try {
  141. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  142. $out_trade_no = $data['out_trade_no'];
  143. // $paytype="alipay";
  144. // $payamount =11;
  145. // $out_trade_no = "202009205f6707697421b23";
  146. //你可以在此编写订单逻辑
  147. //Log::record('Alipay notify ,支付成功');
  148. // 条件一
  149. $orderModel = new \addons\unishop\model\Order(); //($message['out_trade_no']);
  150. $order = $orderModel->where(['out_trade_no' => $out_trade_no])->find();
  151. if (!$order || $order->have_paid != \addons\unishop\model\Order::PAID_NO) {
  152. throw new Exception('订单不存在或已完成');
  153. }
  154. // 条件二
  155. if ($order->total_price > $payamount || $order->total_price < $payamount) {
  156. throw new Exception('金额不一');
  157. }
  158. // 添加行为
  159. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  160. $payTypeString = $paytype == 'alipay' ? \addons\unishop\model\Order::PAY_ALIPAY : \addons\unishop\model\Order::PAY_WXPAY;
  161. Hook::listen('paid_success', $order, ['pay_type' => $payTypeString]);
  162. } catch (Exception $e) {
  163. }
  164. $pay = new \Yansongda\Pay\Pay();
  165. echo $pay->success();
  166. }
  167. /**
  168. * 在线支付
  169. */
  170. public function offline()
  171. {
  172. $orderId = $this->request->get('order_id', 0);
  173. $orderId = Hashids::decodeHex($orderId);
  174. $orderModel = new \addons\unishop\model\Order();
  175. $order = $orderModel->where(['id' => $orderId])->find();
  176. if (!$order) {
  177. $this->error(__('Order does not exist'));
  178. }
  179. try {
  180. Db::startTrans();
  181. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  182. Hook::listen('paid_success', $order, ['pay_type' => \addons\unishop\model\Order::PAY_OFFLINE]);
  183. Db::commit();
  184. } catch (Exception $e) {
  185. Db::rollback();
  186. $this->error($e->getMessage());
  187. }
  188. $this->success('', true);
  189. }
  190. /**
  191. * 微信内H5-JSAPI支付
  192. */
  193. public function jssdkBuildConfig()
  194. {
  195. $app = Wechat::initEasyWechat('payment');
  196. $configData = $app->jssdk->buildConfig(['chooseWXPay'], false, true, false);
  197. $this->success('', $configData);
  198. }
  199. /**
  200. * 支付宝支付
  201. */
  202. public function alipay()
  203. {
  204. $orderId = $this->request->request('order_id', 0);
  205. $orderId = Hashids::decodeHex($orderId);
  206. $orderModel = new \addons\unishop\model\Order();
  207. $order = $orderModel->where(['id' => $orderId])->find();
  208. try {
  209. if (!$order) {
  210. $this->error(__('Order does not exist'));
  211. }
  212. $products = $order->products()->select();
  213. $body = Config::getByName('name')['value'];
  214. foreach ($products as $product) {
  215. $body .= '_' . $product['title'];
  216. }
  217. $params = [
  218. 'amount' => $order->total_price,
  219. 'orderid' => $order->out_trade_no,
  220. 'type' => "alipay",
  221. 'title' => $body,
  222. 'notifyurl' => Config::getByName('ali_notify_url')['value'],
  223. 'returnurl' => Config::getByName('ali_return_url')['value'],
  224. 'method' => "wap",
  225. ];
  226. return Service::submitOrder($params);
  227. } catch (Exception $e) {
  228. $this->error($e->getMessage());
  229. }
  230. }
  231. }