Index.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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->request('amount');
  37. $type = $this->request->request('type');
  38. $method = $this->request->request('method');
  39. if (!$amount || $amount < 0) {
  40. $this->error("支付金额必须大于0");
  41. }
  42. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  43. $this->error("支付类型不能为空");
  44. }
  45. //订单号
  46. $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
  47. //订单标题
  48. $title = '测试订单';
  49. //回调链接
  50. $notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
  51. $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
  52. $response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method);
  53. return $response;
  54. }
  55. /**
  56. * 支付成功,仅供开发测试
  57. */
  58. public function notifyx()
  59. {
  60. $paytype = $this->request->param('paytype');
  61. $pay = Service::checkNotify($paytype);
  62. if (!$pay) {
  63. echo '签名错误';
  64. return;
  65. }
  66. $data = $pay->verify();
  67. try {
  68. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  69. $out_trade_no = $data['out_trade_no'];
  70. //你可以在此编写订单逻辑
  71. } catch (Exception $e) {
  72. }
  73. //下面这句必须要执行,且在此之前不能有任何输出
  74. return $pay->success()->send();
  75. }
  76. /**
  77. * 支付返回,仅供开发测试
  78. */
  79. public function returnx()
  80. {
  81. $paytype = $this->request->param('paytype');
  82. $out_trade_no = $this->request->param('out_trade_no');
  83. $pay = Service::checkReturn($paytype);
  84. if (!$pay) {
  85. $this->error('签名错误', '');
  86. }
  87. //你可以在这里通过out_trade_no去验证订单状态
  88. //但是不可以在此编写订单逻辑!!!
  89. $this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
  90. }
  91. }