PayBeaver.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Created by PayBeaver <merchant.paybeaver.com>
  4. * Version: 2020-12-06
  5. */
  6. namespace App\Http\Controllers\Gateway;
  7. use App\Models\Payment;
  8. use Log;
  9. use Auth;
  10. use Http;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\Request;
  13. use Response;
  14. class PayBeaver extends AbstractPayment
  15. {
  16. private $appId;
  17. private $appSecret;
  18. private $url = 'https://api.paybeaver.com/api/v1/developer';
  19. public function __construct($appId, $appSecret)
  20. {
  21. $this->appId = $appId;
  22. $this->appSecret = $appSecret;
  23. }
  24. public function purchase($request): JsonResponse
  25. {
  26. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  27. $result = $this->createOrder([
  28. 'app_id' => $this->appId,
  29. 'merchant_order_id' => $payment->trade_no,
  30. 'price_amount' => $payment->amount * 100,
  31. 'notify_url' => route('payment.notify', ['method' => 'paybeaver']),
  32. 'return_url' => route('invoice'),
  33. ]);
  34. if (isset($result['message'])) {
  35. Log::warning('创建订单错误:'.$result['message']);
  36. return Response::json(['status' => 'fail', 'message' => '创建订单失败:'.$result['message']]);
  37. }
  38. if (!isset($result['data']) || !isset($result['data']['pay_url'])) {
  39. Log::warning('创建订单错误:未知错误');
  40. return Response::json(['status' => 'fail', 'message' => '创建订单失败:未知错误']);
  41. }
  42. $payment->update(['url' => $result['data']['pay_url']]);
  43. return Response::json(['status' => 'success', 'url' => $result['data']['pay_url'], 'message' => '创建订单成功!']);
  44. }
  45. public function notify($request): void
  46. {
  47. if (!$this->paybeaverVerify($request->post())) {
  48. exit(json_encode(['status' => 400]));
  49. }
  50. $tradeNo = $request->input(['merchant_order_id']);
  51. $payment = Payment::whereTradeNo($tradeNo)->first();
  52. if ($payment) {
  53. $ret = $payment->order->update(['status' => 2]);
  54. if ($ret) {
  55. exit(json_encode(['status' => 200]));
  56. }
  57. }
  58. exit(json_encode(['status' => 500]));
  59. }
  60. protected function createOrder($params)
  61. {
  62. $params['sign'] = $this->sign($params);
  63. return $this->request('/orders', $params);
  64. }
  65. protected function paybeaverVerify($params)
  66. {
  67. // Log::warning('got sign ' . $params['sign']);
  68. // Log::warning('calc sign ' . $this->sign($params));
  69. return hash_equals($params['sign'], $this->sign($params));
  70. }
  71. protected function sign($params)
  72. {
  73. // Log::warning('paybeaver app secret: ' . $this->appSecret);
  74. // Log::warning('query: ' . http_build_query($params) . $this->appSecret);
  75. if (isset($params['sign'])) unset($params['sign']);
  76. ksort($params);
  77. reset($params);
  78. return strtolower(md5(http_build_query($params) . $this->appSecret));
  79. }
  80. protected function request($path, $data) {
  81. $curl = curl_init();
  82. curl_setopt($curl, CURLOPT_URL, "{$this->url}{$path}");
  83. curl_setopt($curl, CURLOPT_POST, 1);
  84. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
  85. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  86. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  87. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  88. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  89. $data = curl_exec($curl);
  90. curl_close($curl);
  91. return json_decode($data, true);
  92. }
  93. }