ApcopayPaymentService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Services\Apcopay;
  3. use Illuminate\Support\Facades\Log;
  4. class ApcopayPaymentService extends ApcopayClient
  5. {
  6. /**
  7. * 创建托管支付页面token
  8. */
  9. public function createHostedPaymentPageToken(array $params): array
  10. {
  11. // 记录详细的请求参数
  12. Log::info('Apcopay createHostedPaymentPageToken request', [
  13. 'params' => $params
  14. ]);
  15. // 确保必要的参数存在
  16. if (!isset($params['BrandId'])) {
  17. $params['BrandId'] = $this->config['brand_code'];
  18. }
  19. // 确保金额是字符串类型
  20. if (isset($params['Amount']) && !is_string($params['Amount'])) {
  21. $params['Amount'] = (string)$params['Amount'];
  22. }
  23. // 发送请求并获取响应
  24. $response = $this->post('/merchanttools/api/hpptoken/create', $params);
  25. // 记录详细的响应
  26. Log::info('Apcopay createHostedPaymentPageToken response', [
  27. 'response' => $response
  28. ]);
  29. return $response;
  30. }
  31. /**
  32. * 生成支付URL
  33. */
  34. public function getPaymentPageUrl(string $token): string
  35. {
  36. // 检查token是否已经是完整的URL
  37. if (strpos($token, 'http') === 0) {
  38. return $token;
  39. }
  40. $baseUrl = rtrim($this->config['api_url'], '/');
  41. return $baseUrl . '/merchanttools/hpp/' . $token;
  42. }
  43. /**
  44. * 查询交易
  45. */
  46. public function queryTransaction(array $params): array
  47. {
  48. // 记录查询参数
  49. Log::info('Apcopay queryTransaction request', [
  50. 'params' => $params
  51. ]);
  52. $response = $this->get('/merchanttools/api/transactions/retrieve', $params);
  53. // 记录响应
  54. Log::info('Apcopay queryTransaction response', [
  55. 'response' => $response
  56. ]);
  57. return $response;
  58. }
  59. /**
  60. * 执行直接支付(提现)
  61. *
  62. * @param array $params 支付参数
  63. * @return array 响应结果
  64. */
  65. public function executeDirectPayment(array $params): array
  66. {
  67. // 记录详细的请求参数
  68. Log::info('Apcopay executeDirectPayment request', [
  69. 'params' => $params
  70. ]);
  71. // 确保必要的参数存在
  72. if (!isset($params['BrandId'])) {
  73. $params['BrandId'] = $this->config['brand_code'];
  74. }
  75. // 确保金额是字符串类型
  76. if (isset($params['Amount']) && !is_string($params['Amount'])) {
  77. $params['Amount'] = (string)$params['Amount'];
  78. }
  79. // 发送请求并获取响应
  80. $response = $this->post('/merchanttools/api/directconnect/execute', $params);
  81. // 记录详细的响应
  82. Log::info('Apcopay executeDirectPayment response', [
  83. 'response' => $response
  84. ]);
  85. return $response;
  86. }
  87. }