Index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace addons\epay\controller;
  3. use addons\epay\library\Service;
  4. use fast\Random;
  5. use think\addons\Controller;
  6. use Yansongda\Pay\Log;
  7. use Yansongda\Pay\Pay;
  8. use Exception;
  9. /**
  10. * 微信支付宝插件首页
  11. *
  12. * 此控制器仅用于开发展示说明和体验,建议自行添加一个新的控制器进行处理返回和回调事件,同时删除此控制器文件
  13. *
  14. * Class Index
  15. * @package addons\epay\controller
  16. */
  17. class Index extends Controller
  18. {
  19. protected $layout = 'default';
  20. protected $config = [];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. }
  25. public function index()
  26. {
  27. $this->view->assign("title", "FastAdmin微信支付宝整合插件");
  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 = 'FastAdmin测试订单';
  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. return Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method);
  52. }
  53. /**
  54. * 支付成功,仅供开发测试
  55. */
  56. public function notifyx()
  57. {
  58. $paytype = $this->request->param('paytype');
  59. $pay = \addons\epay\library\Service::checkNotify($paytype);
  60. if (!$pay) {
  61. echo '签名错误';
  62. return;
  63. }
  64. $data = $pay->verify();
  65. try {
  66. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  67. $out_trade_no = $data['out_trade_no'];
  68. //你可以在此编写订单逻辑
  69. } catch (Exception $e) {
  70. }
  71. echo $pay->success();
  72. }
  73. /**
  74. * 支付返回,仅供开发测试
  75. */
  76. public function returnx()
  77. {
  78. $paytype = $this->request->param('paytype');
  79. $out_trade_no = $this->request->param('out_trade_no');
  80. $pay = \addons\epay\library\Service::checkReturn($paytype);
  81. if (!$pay) {
  82. $this->error('签名错误');
  83. }
  84. //你可以在这里通过out_trade_no去验证订单状态
  85. //但是不可以在此编写订单逻辑!!!
  86. $this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
  87. }
  88. }