25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

272 satır
8.8 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 notify()
  128. {
  129. // 添加行为
  130. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  131. Hook::add('paid_fail', 'addons\\unishop\\behavior\\Order');
  132. $paytype = $this->request->param('paytype');
  133. $pay = Service::checkNotify($paytype);
  134. if (!$pay) {
  135. echo '签名错误';
  136. return;
  137. }
  138. $data = $pay->verify();
  139. try {
  140. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  141. $out_trade_no = $data['out_trade_no'];
  142. // $paytype="alipay";
  143. // $payamount =11;
  144. // $out_trade_no = "202009205f6707697421b23";
  145. //你可以在此编写订单逻辑
  146. //Log::record('Alipay notify ,支付成功');
  147. // 条件一
  148. $orderModel = new \addons\unishop\model\Order(); //($message['out_trade_no']);
  149. $order = $orderModel->where(['out_trade_no' => $out_trade_no])->find();
  150. if (!$order || $order->have_paid != \addons\unishop\model\Order::PAID_NO) {
  151. throw new Exception('订单不存在或已完成');
  152. }
  153. // 条件二
  154. if ($order->total_price > $payamount || $order->total_price < $payamount) {
  155. throw new Exception('金额不一');
  156. }
  157. // 添加行为
  158. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  159. $payTypeString = $paytype == 'alipay' ? \addons\unishop\model\Order::PAY_ALIPAY : \addons\unishop\model\Order::PAY_WXPAY;
  160. Hook::listen('paid_success', $order, ['pay_type' => $payTypeString]);
  161. } catch (Exception $e) {
  162. }
  163. $pay = new \Yansongda\Pay\Pay();
  164. echo $pay->success();
  165. }
  166. /**
  167. * 在线支付
  168. */
  169. public function offline()
  170. {
  171. $orderId = $this->request->get('order_id', 0);
  172. $orderId = Hashids::decodeHex($orderId);
  173. $orderModel = new \addons\unishop\model\Order();
  174. $order = $orderModel->where(['id' => $orderId])->find();
  175. if (!$order) {
  176. $this->error(__('Order does not exist'));
  177. }
  178. try {
  179. Db::startTrans();
  180. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  181. Hook::listen('paid_success', $order, ['pay_type' => \addons\unishop\model\Order::PAY_OFFLINE]);
  182. Db::commit();
  183. } catch (Exception $e) {
  184. Db::rollback();
  185. $this->error($e->getMessage());
  186. }
  187. $this->success('', true);
  188. }
  189. /**
  190. * 微信内H5-JSAPI支付
  191. */
  192. public function jssdkBuildConfig()
  193. {
  194. $app = Wechat::initEasyWechat('payment');
  195. $configData = $app->jssdk->buildConfig(['chooseWXPay'], false, true, false);
  196. $this->success('', $configData);
  197. }
  198. /**
  199. * 支付宝支付
  200. */
  201. public function alipay()
  202. {
  203. $orderId = $this->request->request('order_id', 0);
  204. $orderId = Hashids::decodeHex($orderId);
  205. $orderModel = new \addons\unishop\model\Order();
  206. $order = $orderModel->where(['id' => $orderId])->find();
  207. try {
  208. if (!$order) {
  209. $this->error(__('Order does not exist'));
  210. }
  211. $products = $order->products()->select();
  212. $body = Config::getByName('name')['value'];
  213. foreach ($products as $product) {
  214. $body .= '_' . $product['title'];
  215. }
  216. $params = [
  217. 'amount' => $order->total_price,
  218. 'orderid' => $order->out_trade_no,
  219. 'type' => "alipay",
  220. 'title' => $body,
  221. 'notifyurl' => Config::getByName('ali_notify_url')['value'],
  222. 'returnurl' => Config::getByName('ali_return_url')['value'],
  223. 'method' => "wap",
  224. ];
  225. return Service::submitOrder($params);
  226. } catch (Exception $e) {
  227. $this->error($e->getMessage());
  228. }
  229. }
  230. }