CMS使用的是企业微信/支付宝接口如何使用XorPay?

需要修改到的文件:
/addons/cms/model/Order.php
需要增加的文件:
/addons/xorpay/controller/Cms.php

Order.php修改内容
将下面代码增加内容:

  1. if (!$order || (time() - $order->createtime) > 1800) {
  2. $orderid = date("YmdHis") . mt_rand(100000, 999999);
  3. $data = [
  4. 'user_id' => $auth->id ? $auth->id : 0,
  5. 'orderid' => $orderid,
  6. 'archives_id' => $archives->id,
  7. 'title' => "付费阅读",
  8. 'amount' => $archives->price,
  9. 'payamount' => 0,
  10. 'paytype' => $paytype,
  11. 'ip' => $request->ip(),
  12. 'useragent' => substr($request->server('HTTP_USER_AGENT'), 0, 255),
  13. 'status' => 'created'
  14. ];
  15. $order = Order::create($data);
  16. } else {
  17. if ($order->amount != $archives->price) {
  18. $order->amount = $archives->price;
  19. $order->save();
  20. }
  21. }

修改为如下:

  1. if (!$order || (time() - $order->createtime) > 1800) {
  2. $orderid = date("YmdHis") . mt_rand(100000, 999999);
  3. $data = [
  4. 'user_id' => $auth->id ? $auth->id : 0,
  5. 'orderid' => $orderid,
  6. 'archives_id' => $archives->id,
  7. 'title' => "付费阅读",
  8. 'amount' => $archives->price,
  9. 'payamount' => 0,
  10. 'paytype' => $paytype,
  11. 'ip' => $request->ip(),
  12. 'useragent' => substr($request->server('HTTP_USER_AGENT'), 0, 255),
  13. 'status' => 'created'
  14. ];
  15. $order = Order::create($data);
  16. } else {
  17. if ($order->amount != $archives->price) {
  18. $order->amount = $archives->price;
  19. $order->save();
  20. }
  21. //存在订单的话更新一下订单号 jianbs 19.9.14
  22. $orderid = $orderid = date("YmdHis") . mt_rand(100000, 999999);
  23. Order::where('id',$order['id'])->update(['orderid'=>$orderid]);
  24. $order['orderid'] = $orderid;
  25. }

将下面代码注释:

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

注释后在代码下面新增代码:

  1. //使用xorpay 支付
  2. $xorpay = get_addon_info('xorpay');
  3. if ($xorpay && $xorpay['state']) {
  4. $notifyurl = addon_url("xorpay/cms/notifyit", [], true, true);
  5. $returnurl = addon_url("xorpay/cms/returnit", [], true, true);
  6. //自定义附加传递的信息,例如可以用来传递会员ID、会员账号、商品ID等等
  7. $extend = 'article';//充值类型
  8. $more = $order->archives_id;//文章ID
  9. $order_uid = $auth->id;
  10. //发起支付,并跳转到支付页面
  11. \addons\xorpay\library\Service::submitOrder($order->amount, $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/新增一个Cms.php控制器
Cms.php文件代码如下:

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