会员余额充值插件对接XorPay接口

FastAdmin 会员余额充值插件快速对接xorpay支付插件的方法

需要修改的文件:
/addons/recharge/model/Order.php
需要新增的文件:
/addons/xorpay/controller/Recharge.php

Order.php修改内容
在下面的代码里增加内容:

  1. if (!$order) {
  2. $orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  3. $data = [
  4. 'orderid' => $orderid,
  5. 'user_id' => $user_id,
  6. 'amount' => $money,
  7. 'payamount' => 0,
  8. 'paytype' => $paytype,
  9. 'ip' => $request->ip(),
  10. 'useragent' => $request->server('HTTP_USER_AGENT'),
  11. 'status' => 'created'
  12. ];
  13. $order = self::create($data);
  14. }

修改为如下代码:

  1. if (!$order) {
  2. $orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  3. $data = [
  4. 'orderid' => $orderid,
  5. 'user_id' => $user_id,
  6. 'amount' => $money,
  7. 'payamount' => 0,
  8. 'paytype' => $paytype,
  9. 'ip' => $request->ip(),
  10. 'useragent' => $request->server('HTTP_USER_AGENT'),
  11. 'status' => 'created'
  12. ];
  13. $order = self::create($data);
  14. }else{
  15. //存在订单的话更新一下订单号 jianbs 19.9.14
  16. $orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  17. Order::where('id',$order['id'])->update(['orderid'=>$orderid]);
  18. $order['orderid'] = $orderid;
  19. }

将下面代码注释:

  1. /*$epay = get_addon_info('epay');
  2. if ($epay && $epay['state']) {
  3. $notifyurl = $request->root(true) . '/index/recharge/epay/type/notify/paytype/' . $paytype;
  4. $returnurl = $request->root(true) . '/index/recharge/epay/type/return/paytype/' . $paytype;
  5. \addons\epay\library\Service::submitOrder($money, $order->orderid, $paytype, "充值{$money}元", $notifyurl, $returnurl);
  6. exit;
  7. } else {
  8. $result = \think\Hook::listen('recharge_order_submit', $order);
  9. if (!$result) {
  10. throw new Exception("请先在后台安装并配置微信支付宝整合插件");
  11. }
  12. }*/

在注释的代码底部增加如下代码:

  1. //使用xorpay 支付
  2. $xorpay = get_addon_info('xorpay');
  3. if ($xorpay && $xorpay['state']) {
  4. $notifyurl = addon_url("xorpay/recharge/notifyit", [], true, true);
  5. $returnurl = addon_url("xorpay/recharge/returnit", [], true, true);
  6. //自定义附加传递的信息,例如可以用来传递会员ID、会员账号、商品ID等等
  7. $extend = 'recharge';//充值类型
  8. $more = '';
  9. $order_uid = $auth->id;
  10. //发起支付,并跳转到支付页面
  11. \addons\xorpay\library\Service::submitOrder($money, $order->orderid,'余额充值', $paytype, $notifyurl, $returnurl, $extend,$more,$order_uid);
  12. exit;
  13. } else {
  14. $result = \think\Hook::listen('recharge_order_submit', $order);
  15. if (!$result) {
  16. throw new Exception("支付功能不存在或未开启");
  17. }
  18. }

在/addons/xorpay/controller/新增一个Recharge.php控制器
Recharge.php文件代码如下:

  1. namespace addons\xorpay\controller;
  2. use addons\xorpay\model\Order;
  3. use think\addons\Controller;
  4. /**
  5. * 充值处理类
  6. *
  7. * Class Index
  8. * @package addons\xorpay\controller
  9. */
  10. class Recharge extends Controller
  11. {
  12. protected $layout = 'default';
  13. protected $config = [];
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->config = get_addon_config('xorpay');
  18. }
  19. public function index()
  20. {
  21. echo 'Is OK!';die;
  22. }
  23. /**
  24. * 通知回调
  25. */
  26. public function notifyit()
  27. {
  28. $aoid = $this->request->request('aoid', '');
  29. $order_id = $this->request->request('order_id', '');
  30. $pay_price = $this->request->request('pay_price', '');
  31. $pay_time = $this->request->request('pay_time', '');
  32. $sign = $this->request->request('sign', '');
  33. $secret = $this->config['app_secret'];
  34. //请求xorpay 的签名 和回调回来的签名方式是不同的
  35. //回调签名方式:签名, 参数 aoid + order_id + pay_price + pay_time + app secret 顺序拼接后 MD5
  36. if ($sign != md5(join('',array($aoid, $order_id, $pay_price, $pay_time, $secret)))) {
  37. $this->error('签名错误');
  38. exit();
  39. }
  40. // 签名验证成功,更新订单数据
  41. try {
  42. $params = [
  43. 'paytime'=>$pay_time,
  44. 'status'=>'settled',
  45. ];
  46. if($aoid)
  47. {
  48. $params['aoid'] = $aoid;
  49. }
  50. Order::where('out_order_id',$order_id)->update($params);
  51. //更新会员充值订单
  52. \addons\recharge\model\Order::settle($order_id, $pay_price);
  53. } catch (Exception $e) {
  54. }
  55. //注意只能输出一个success
  56. echo "success";
  57. return;
  58. }
  59. /**
  60. * 支付成功的返回
  61. */
  62. public function returnit()
  63. {
  64. $order_id = $this->request->request('order_id', '');
  65. $sign = $this->request->request('sign', '');
  66. $config = get_addon_config('xorpay');
  67. $order = Order::get($order_id);
  68. if(count($order) == 0)
  69. {
  70. $this->error('查询订单失败');
  71. exit();
  72. }
  73. if ($sign != md5(join('',array($order['name'], $order['pay_type'], $order['price'], $order['out_order_id'], $order['notifyurl'], $config['app_secret'])))) {
  74. $this->error('签名错误');
  75. exit();
  76. }
  77. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  78. $this->success("恭喜你!充值成功!", url("index/recharge/recharge"));
  79. return;
  80. }
  81. }