Index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace addons\eepay\controller;
  3. use fast\Random;
  4. use think\addons\Controller;
  5. use Exception;
  6. /**
  7. * 微信支付宝插件首页
  8. *
  9. * 此控制器仅用于开发展示说明和体验,建议自行添加一个新的控制器进行处理返回和回调事件,同时删除此控制器文件
  10. *
  11. * Class Index
  12. * @package addons\epay\controller
  13. */
  14. class Index extends Controller
  15. {
  16. protected $layout = 'default';
  17. protected $config = [];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!config("app_debug")) {
  22. $this->error("仅在开发环境下查看");
  23. }
  24. }
  25. public function index()
  26. {
  27. $this->view->assign("title", "微信支付宝整合插件");
  28. return $this->view->fetch();
  29. }
  30. /**
  31. * 体验,仅供开发测试
  32. */
  33. public function experience()
  34. {
  35. $amount = $this->request->request('amount');
  36. $type = $this->request->request('type');
  37. $method = $this->request->request('method');
  38. if (!$amount || $amount < 0) {
  39. $this->error("支付金额必须大于0");
  40. }
  41. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  42. $this->error("支付类型不能为空");
  43. }
  44. //订单号
  45. $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
  46. //订单标题
  47. $title = '测试订单';
  48. //回调链接
  49. $notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
  50. $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
  51. $response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method);
  52. return $response;
  53. }
  54. /**
  55. * 支付成功,仅供开发测试
  56. */
  57. public function notifyx()
  58. {
  59. $paytype = $this->request->param('paytype');
  60. $pay = Service::checkNotify($paytype);
  61. if (!$pay) {
  62. echo '签名错误';
  63. return;
  64. }
  65. $data = $pay->verify();
  66. try {
  67. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  68. $out_trade_no = $data['out_trade_no'];
  69. //你可以在此编写订单逻辑
  70. } catch (Exception $e) {
  71. }
  72. echo $pay->success();
  73. }
  74. /**
  75. * 支付返回,仅供开发测试
  76. */
  77. public function returnx()
  78. {
  79. $paytype = $this->request->param('paytype');
  80. $out_trade_no = $this->request->param('out_trade_no');
  81. $pay = Service::checkReturn($paytype);
  82. if (!$pay) {
  83. $this->error('签名错误', '');
  84. }
  85. //你可以在这里通过out_trade_no去验证订单状态
  86. //但是不可以在此编写订单逻辑!!!
  87. $this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
  88. }
  89. }