BitpayX.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use Auth;
  4. use Http;
  5. use Illuminate\Http\JsonResponse;
  6. use Log;
  7. use Response;
  8. class BitpayX extends AbstractPayment
  9. {
  10. public function purchase($request): JsonResponse
  11. {
  12. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  13. $data = [
  14. 'merchant_order_id' => $payment->trade_no,
  15. 'price_amount' => $payment->amount,
  16. 'price_currency' => 'CNY',
  17. 'title' => '支付单号:'.$payment->trade_no,
  18. 'description' => sysConfig('subject_name') ?: sysConfig('website_name'),
  19. 'callback_url' => route('payment.notify', ['method' => 'bitpayx']),
  20. 'success_url' => route('invoice'),
  21. 'cancel_url' => route('invoice'),
  22. 'token' => $this->sign($payment->trade_no),
  23. ];
  24. $result = $this->sendRequest($data);
  25. if ($result['status'] === 200 || $result['status'] === 201) {
  26. $result['payment_url'] .= '&lang=zh';
  27. $payment->update(['url' => $result['payment_url']]);
  28. return Response::json(['status' => 'success', 'url' => $result['payment_url'], 'message' => '创建订单成功!']);
  29. }
  30. Log::warning('创建订单错误:'.var_export($result, true));
  31. return Response::json(['status' => 'fail', 'message' => '创建订单失败!'.$result['error']]);
  32. }
  33. private function sign($tradeNo): string
  34. {
  35. $data = [
  36. 'merchant_order_id' => $tradeNo,
  37. 'secret' => sysConfig('bitpay_secret'),
  38. 'type' => 'FIAT',
  39. ];
  40. return $this->aliStyleSign($data, sysConfig('bitpay_secret'));
  41. }
  42. private function sendRequest($data, $type = 'createOrder')
  43. {
  44. $client = Http::baseUrl('https://api.mugglepay.com/v1/')
  45. ->timeout(15)
  46. ->withHeaders([
  47. 'token' => sysConfig('bitpay_secret'),
  48. 'content-type' => 'application/json',
  49. ]);
  50. if ($type === 'query') {
  51. $response = $client->get('orders/merchant_order_id/status?id='.$data['merchant_order_id']);
  52. } else {// Create Order
  53. $response = $client->post('orders', ['body' => json_encode($data)]);
  54. }
  55. if ($response->failed()) {
  56. Log::error('BitPayX请求支付错误:'.var_export($response, true));
  57. }
  58. return $response->json();
  59. }
  60. //Todo: Postman虚拟测试通过,需要真实数据参考验证
  61. public function notify($request): void
  62. {
  63. $tradeNo = $request->input(['merchant_order_id']);
  64. if ($request->input(['status']) === 'PAID' && hash_equals($this->sign($tradeNo), $request->input(['token']))) {
  65. if ($this->paymentReceived($tradeNo)) {
  66. exit(json_encode(['status' => 200]));
  67. }
  68. } else {
  69. Log::info('BitpayX:交易失败');
  70. }
  71. exit(json_encode(['status' => 400]));
  72. }
  73. }