PaycenterPayment.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Repositories\Service\Payment;
  3. use App\Repositories\ConfigRepoistory;
  4. use App\Repositories\Service\CommonService;
  5. class PaycenterPayment extends PaymentInterface
  6. {
  7. /**
  8. * @var null
  9. */
  10. private static $_instance = null;
  11. /**
  12. * @return PaycenterPayment|null
  13. */
  14. public static function getInstance()
  15. {
  16. if (is_null(self::$_instance)) {
  17. self::$_instance = new self();
  18. }
  19. return self::$_instance;
  20. }
  21. public function createPayLink($data = array())
  22. {
  23. $payUrl = make(ConfigRepoistory::class)
  24. ->get('pay_notify_url', 'sys', '');
  25. if (!$payUrl) {
  26. $this->_lastError = '网络错误';
  27. payLogger(__CLASS__, '网络错误');
  28. return '';
  29. }
  30. $notifyUrl = trim($payUrl, '/');
  31. $notifyUrl .= '/notify/' . $data['type'] . '/' . $data['orderId'];
  32. $info = [
  33. 'merchant_code' => env('PAY_CENTER_MERCHANT_CODE'),
  34. 'payment_id' => $data['payId'],
  35. 'amount' => $data['money'],
  36. 'order_id' => $data['orderSn'],
  37. 'notify_url' => $notifyUrl,
  38. 'redirect_url' => $payUrl,
  39. 'product_name' => 'DK',
  40. ];
  41. $info['signature'] = strtoupper(md5($this->getSign($info) . "key=" . env('PAY_CENTER_KEY')));
  42. $info['ip'] = make(CommonService::class)->getIp();
  43. $res = $this->httpPost(env('PAY_CENTER_URL') . '/api/pay/url', ['data' => $info]);
  44. if (empty($res)) {
  45. payLogger(__CLASS__, '获取支付中心链接失败' . $res);
  46. return '';
  47. }
  48. $res = json_decode($res, true);
  49. if (empty($res) || $res['code'] == 500) {
  50. payLogger(__CLASS__, '获取支付中心链接异常:' . $res['message']);
  51. return '';
  52. }
  53. return $res['data']['pay_url'];
  54. }
  55. public function notify(&$orderSn, &$tradeNo, &$money)
  56. {
  57. $data = request()->all();
  58. if ($data['status'] != 2) {
  59. payLogger(__CLASS__, '回调订单状态失败!'.json_encode($data));
  60. return false;
  61. }
  62. $sendData = [
  63. 'merchant_code' => env('PAY_CENTER_MERCHANT_CODE'),
  64. 'status' => $data['status'],
  65. 'order_id' => $data['order_id'],
  66. 'trade_no' => $data['trade_no'],
  67. 'amount' => $data['amount'],
  68. 'real_amount' => $data['real_amount'],
  69. 'product_name' => $data['product_name'],
  70. 'notify_url' => $data['notify_url'],
  71. 'redirect_url' => $data['redirect_url'],
  72. ];
  73. $sign = strtoupper(md5($this->getSign($sendData) . 'key=' . env('PAY_CENTER_KEY')));
  74. if ($sign != $data['signature']) {
  75. $this->_lastError = '验证失败!';
  76. payLogger(__CLASS__, '验证失败!'.json_encode($data));
  77. return false;
  78. }
  79. $money = $data['amount'];
  80. $orderSn = $data['order_id'];
  81. $tradeNo = $data['trade_no'];
  82. return true;
  83. }
  84. }