You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

270 lines
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\config\AliPayConfig;
  10. use addons\epay\library\Service;
  11. use addons\nzf\AliPay;
  12. use addons\unishop\extend\Ali;
  13. use addons\unishop\extend\Hashids;
  14. use addons\unishop\extend\Wechat;
  15. use addons\unishop\model\Config;
  16. use think\Db;
  17. use think\Exception;
  18. use think\Hook;
  19. use think\Log;
  20. class Pay extends Base
  21. {
  22. protected $noNeedLogin = ['getPayType', 'notify','unify', 'authRedirect', 'alipay', 'alinotify'];
  23. /**
  24. * 获取支付类型
  25. */
  26. public function getPayType()
  27. {
  28. $platfrom = $this->request->header('platform');
  29. $type = [];
  30. $offline = Config::getByName('offline_pay')['value'] == 1 ? true : false;
  31. switch ($platfrom) {
  32. case 'APP-PLUS';
  33. $type = ['alipay' => true, 'wxpay' => true, 'offline' => $offline];
  34. break;
  35. case 'H5':
  36. $type = ['alipay' => true, 'wxpay' => true, 'offline' => $offline];
  37. // 如果是微信内访问 公众号等
  38. if (Wechat::h5InWechat()) {
  39. $type['alipay'] = false;
  40. }
  41. break;
  42. case 'MP-WEIXIN':
  43. $type = ['alipay' => false, 'wxpay' => true, 'offline' => $offline];
  44. break;
  45. case 'MP-ALIPAY':
  46. $type = ['alipay' => true, 'wxpay' => false, 'offline' => $offline];
  47. break;
  48. case 'MP-BAIDU':
  49. $type = ['alipay' => false, 'wxpay' => false, 'offline' => $offline];
  50. break;
  51. case 'MP-TOUTIAO':
  52. $type = ['alipay' => false, 'wxpay' => false, 'offline' => $offline];
  53. break;
  54. }
  55. $this->success('', $type);
  56. }
  57. /**
  58. * 微信统一下单接口
  59. */
  60. public function unify()
  61. {
  62. $orderId = $this->request->request('order_id', 0);
  63. $orderId = Hashids::decodeHex($orderId);
  64. $orderModel = new \addons\unishop\model\Order();
  65. $order = $orderModel->where(['id' => $orderId])->find();
  66. try {
  67. if (!$order) {
  68. $this->error(__('Order does not exist'));
  69. }
  70. $products = $order->products()->select();
  71. $body = Config::getByName('name')['value'];
  72. foreach ($products as $product) {
  73. $body .= '_' . $product['title'];
  74. }
  75. //MWEB
  76. $platfrom = $this->request->header('platform', 'MP-WEIXIN');
  77. switch ($platfrom) {
  78. case 'MP-WEIXIN':
  79. $trade_type = 'JSAPI';
  80. break;
  81. case 'H5':
  82. case 'APP-PLUS':
  83. $trade_type = 'MWEB';
  84. break;
  85. }
  86. // 如果是微信内访问 公众号等
  87. if (Wechat::h5InWechat()) {
  88. $trade_type = 'wap';
  89. }
  90. $params = [
  91. 'amount' => $order['total_price'],
  92. 'orderid' => $order->out_trade_no,
  93. 'type' => "wechat",
  94. 'title' => $body,
  95. 'notifyurl' => AliPayConfig::getByName('notify_url'),
  96. 'returnurl' => AliPayConfig::getByName('ali_return_url'),
  97. 'trade_type' => $trade_type,
  98. ];
  99. $this->success("", Service::submitOrder($params));
  100. } catch (Exception $e) {
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. /**
  105. * 支付通知回调
  106. */
  107. public function notify()
  108. {
  109. // 添加行为
  110. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  111. Hook::add('paid_fail', 'addons\\unishop\\behavior\\Order');
  112. $paytype = $this->request->param('type');
  113. $string = date('Y-m-d H:i:s', time()) . PHP_EOL;
  114. $body = file_get_contents("php://input");
  115. if (empty($body) == false) {
  116. $string .= 'body : ' . json_encode($body) . PHP_EOL;
  117. }
  118. $pay = Service::checkNotify($paytype);
  119. if (!$pay) {
  120. $string.="签名错误" . PHP_EOL;
  121. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/pay_".date("d"). '.log',$string , FILE_APPEND);
  122. echo '签名错误';
  123. return;
  124. }
  125. $data = $pay->verify();
  126. try {
  127. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  128. $out_trade_no = $data['out_trade_no'];
  129. // $paytype="alipay";
  130. // $payamount =11;
  131. // $out_trade_no = "202009205f6707697421b23";
  132. //你可以在此编写订单逻辑
  133. //Log::record('Alipay notify ,支付成功');
  134. // 条件一
  135. $orderModel = new \addons\unishop\model\Order(); //($message['out_trade_no']);
  136. $order = $orderModel->where(['out_trade_no' => $out_trade_no])->find();
  137. if (!$order || $order->have_paid != \addons\unishop\model\Order::PAID_NO) {
  138. $string.="订单不存在或已完成" . PHP_EOL;
  139. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/pay_".date("d"). '.log',$string , FILE_APPEND);
  140. throw new Exception('订单不存在或已完成');
  141. }
  142. // 条件二
  143. if ($order->total_price > $payamount || $order->total_price < $payamount) {
  144. $string.="金额不一" . PHP_EOL;
  145. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/pay_".date("d"). '.log',$string , FILE_APPEND);
  146. throw new Exception('金额不一');
  147. }
  148. // 添加行为
  149. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  150. $payTypeString = $paytype == 'alipay' ? \addons\unishop\model\Order::PAY_ALIPAY : \addons\unishop\model\Order::PAY_WXPAY;
  151. Hook::listen('paid_success', $order, ['pay_type' => $payTypeString]);
  152. } catch (Exception $e) {
  153. $string.=$e->getMessage() . PHP_EOL;
  154. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/pay_".date("d"). '.log',$string , FILE_APPEND);
  155. }
  156. $pay = new \Yansongda\Pay\Pay();
  157. echo $pay->success();
  158. }
  159. /**
  160. * 在线支付
  161. */
  162. public function offline()
  163. {
  164. $orderId = $this->request->get('order_id', 0);
  165. $orderId = Hashids::decodeHex($orderId);
  166. $orderModel = new \addons\unishop\model\Order();
  167. $order = $orderModel->where(['id' => $orderId])->find();
  168. if (!$order) {
  169. $this->error(__('Order does not exist'));
  170. }
  171. try {
  172. Db::startTrans();
  173. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  174. Hook::listen('paid_success', $order, ['pay_type' => \addons\unishop\model\Order::PAY_OFFLINE]);
  175. Db::commit();
  176. } catch (Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. $this->success('', true);
  181. }
  182. /**
  183. * 微信内H5-JSAPI支付
  184. */
  185. public function jssdkBuildConfig()
  186. {
  187. $app = Wechat::initEasyWechat('payment');
  188. $configData = $app->jssdk->buildConfig(['chooseWXPay'], false, true, false);
  189. $this->success('', $configData);
  190. }
  191. /**
  192. * 支付宝支付
  193. */
  194. public function alipay()
  195. {
  196. $orderId = $this->request->request('order_id', 0);
  197. $orderId = Hashids::decodeHex($orderId);
  198. $orderModel = new \addons\unishop\model\Order();
  199. $order = $orderModel->where(['id' => $orderId])->find();
  200. try {
  201. if (!$order) {
  202. $this->error(__('Order does not exist'));
  203. }
  204. $products = $order->products()->select();
  205. $body = Config::getByName('name')['value'];
  206. foreach ($products as $product) {
  207. $body .= '_' . $product['title'];
  208. }
  209. $params = [
  210. 'amount' => $order->total_price,
  211. 'orderid' => $order->out_trade_no,
  212. 'type' => "alipay",
  213. 'title' => $body,
  214. 'notify_url' => AliPayConfig::getByName('ali_notify_url'),
  215. 'return_url' => AliPayConfig::getByName('ali_return_url'),
  216. 'method' => "wap",
  217. ];
  218. return Service::submitOrder($params);
  219. } catch (Exception $e) {
  220. $this->error($e->getMessage());
  221. }
  222. }
  223. // public function notify(){
  224. // $order = [
  225. // "order_id"=>"202009275f7076e5c6f2f23",
  226. // "total_fee"=>11,
  227. // "memo"=>'取消订单',
  228. // "refund_fee"=>11,
  229. //
  230. // ];//order_id:订单ID name:订单名称 total_fee:总金额-元 refund_fee退款金额
  231. // AliPay::cancelOrder($order);
  232. //
  233. // }
  234. }