Index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\epay\controller;
  3. use addons\epay\library\Service;
  4. use fast\Random;
  5. use think\addons\Controller;
  6. use Exception;
  7. /**
  8. * 微信支付宝整合插件首页
  9. *
  10. * 此控制器仅用于开发展示说明和测试,请自行添加一个新的控制器进行处理返回和回调事件,同时删除此控制器文件
  11. *
  12. * Class Index
  13. * @package addons\epay\controller
  14. */
  15. class Index extends Controller
  16. {
  17. protected $layout = 'default';
  18. protected $config = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. if (!config("app_debug")) {
  23. $this->error("仅在开发环境下查看");
  24. }
  25. }
  26. public function index()
  27. {
  28. $this->view->assign("title", "微信支付宝整合");
  29. return $this->view->fetch();
  30. }
  31. /**
  32. * 体验,仅供开发测试
  33. */
  34. public function experience()
  35. {
  36. $amount = $this->request->post('amount');
  37. $type = $this->request->post('type');
  38. $method = $this->request->post('method');
  39. $openid = $this->request->post('openid', "");
  40. if (!$amount || $amount < 0) {
  41. $this->error("支付金额必须大于0");
  42. }
  43. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  44. $this->error("支付类型不能为空");
  45. }
  46. if (in_array($method, ['miniapp', 'mp']) && !$openid) {
  47. $this->error("openid不能为空");
  48. }
  49. //订单号
  50. $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
  51. //订单标题
  52. $title = '测试订单';
  53. //回调链接
  54. $notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
  55. $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
  56. $response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method, $openid);
  57. return $response;
  58. }
  59. /**
  60. * 支付成功,仅供开发测试
  61. */
  62. public function notifyx()
  63. {
  64. $paytype = $this->request->param('paytype');
  65. $pay = Service::checkNotify($paytype);
  66. if (!$pay) {
  67. return json(['code' => 'FAIL', 'message' => '失败'], 500, ['Content-Type' => 'application/json']);
  68. }
  69. // 获取回调数据,V3和V2的回调接收不同
  70. $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
  71. try {
  72. //微信支付V3返回和V2不同
  73. if (Service::isVersionV3() && $paytype === 'wechat') {
  74. $data = $data['resource']['ciphertext'];
  75. $data['total_fee'] = $data['amount']['total'];
  76. }
  77. \think\Log::record($data);
  78. //获取支付金额、订单号
  79. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  80. $out_trade_no = $data['out_trade_no'];
  81. \think\Log::record("回调成功,订单号:{$out_trade_no},金额:{$payamount}");
  82. //你可以在此编写订单逻辑
  83. } catch (Exception $e) {
  84. \think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
  85. }
  86. //下面这句必须要执行,且在此之前不能有任何输出
  87. if (Service::isVersionV3()) {
  88. return $pay->success()->getBody()->getContents();
  89. } else {
  90. return $pay->success()->send();
  91. }
  92. }
  93. /**
  94. * 支付返回,仅供开发测试
  95. */
  96. public function returnx()
  97. {
  98. $paytype = $this->request->param('paytype');
  99. $out_trade_no = $this->request->param('out_trade_no');
  100. $pay = Service::checkReturn($paytype);
  101. if (!$pay) {
  102. $this->error('签名错误', '');
  103. }
  104. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  105. $this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
  106. }
  107. }