JdPayService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Services\Payment;
  3. use App\Services\BaseService;
  4. use GuzzleHttp\Client;
  5. class JdPayService extends BaseService
  6. {
  7. public const CHANNEL = 'JDpay';
  8. public const PAY_STATUS_SUCCESS = '3';
  9. public const PAY_STATUS_FAIL = '4';
  10. public const REMIT_STATUS_SUCCESS = '2';
  11. public const REMIT_STATUS_FAIL = '3';
  12. public static function getMerchantId()
  13. {
  14. return PaymentProviderConfig::get('jd', 'mch_id');
  15. }
  16. public static function getSecret()
  17. {
  18. return PaymentProviderConfig::get('jd', 'key');
  19. }
  20. public static function getNotifyUrl(): string
  21. {
  22. return rtrim(config('app.url'), '/') . '/api/pay/harvest';
  23. }
  24. public static function getRemitNotifyUrl(): string
  25. {
  26. return rtrim(config('app.url'), '/') . '/api/pay/notify';
  27. }
  28. public static function getClient(): Client
  29. {
  30. return new Client([
  31. 'timeout' => 10.0,
  32. ]);
  33. }
  34. public static function isChannel(?string $channel): bool
  35. {
  36. return strtolower((string)$channel) === strtolower(self::CHANNEL)
  37. || strtolower((string)$channel) === 'jdpay'
  38. || (string)$channel === 'JD钱包';
  39. }
  40. public static function canUserRecharge($userId): bool
  41. {
  42. $allowedUserIds = array_values(array_filter(array_map(
  43. 'trim',
  44. explode(',', PaymentProviderConfig::get('jd', 'recharge_user_ids'))
  45. )));
  46. if (empty($allowedUserIds)) {
  47. return true;
  48. }
  49. return in_array((string)$userId, $allowedUserIds, true);
  50. }
  51. public static function amount($amount): string
  52. {
  53. return number_format((float)$amount, 2, '.', '');
  54. }
  55. public static function signature(array $values, ?string $key = null): string
  56. {
  57. $values[] = ($key ?? self::getSecret());
  58. return strtoupper(md5(implode('&', $values)));
  59. }
  60. public static function pay($amount, string $orderNo): array
  61. {
  62. $config = PaymentProviderConfig::snapshot('jd');
  63. $amount = self::amount($amount);
  64. $data = [
  65. 'userCode' => $config['mch_id'],
  66. 'orderCode' => $orderNo,
  67. 'amount' => $amount,
  68. 'callbackUrl' => self::getNotifyUrl(),
  69. ];
  70. $data['sign'] = self::signature([$orderNo, $amount, $data['userCode']], $config['key']);
  71. return self::post($config['pay_gateway'], $data);
  72. }
  73. public static function queryPayOrder(string $orderNo = '', string $customerOrderNo = ''): array
  74. {
  75. $config = PaymentProviderConfig::snapshot('jd');
  76. $data = [
  77. 'userCode' => $config['mch_id'],
  78. 'orderCode' => $orderNo,
  79. 'customerOrderCode' => $customerOrderNo,
  80. ];
  81. $data['sign'] = self::signature([$data['orderCode'], $data['customerOrderCode'], $data['userCode']], $config['key']);
  82. return self::post($config['pay_query_gateway'], $data);
  83. }
  84. public static function remit($amount, string $orderNo, string $address): array
  85. {
  86. $config = PaymentProviderConfig::snapshot('jd');
  87. $amount = self::amount($amount);
  88. $data = [
  89. 'userCode' => $config['mch_id'],
  90. 'orderCode' => $orderNo,
  91. 'amount' => $amount,
  92. 'address' => $address,
  93. 'callbackUrl' => self::getRemitNotifyUrl(),
  94. ];
  95. $data['sign'] = self::signature([$orderNo, $amount, $address, $data['userCode']], $config['key']);
  96. return self::post($config['remit_gateway'], $data);
  97. }
  98. public static function queryRemitOrder(string $orderNo = '', string $customerOrderNo = ''): array
  99. {
  100. $config = PaymentProviderConfig::snapshot('jd');
  101. $data = [
  102. 'userCode' => $config['mch_id'],
  103. 'orderCode' => $orderNo,
  104. 'customerOrderCode' => $customerOrderNo,
  105. ];
  106. $data['sign'] = self::signature([$data['orderCode'], $data['customerOrderCode'], $data['userCode']], $config['key']);
  107. return self::post($config['remit_query_gateway'], $data);
  108. }
  109. public static function balance(): array
  110. {
  111. $config = PaymentProviderConfig::snapshot('jd');
  112. $timestamp = (string)round(microtime(true) * 1000);
  113. $userCode = $config['mch_id'];
  114. $sign = self::signature([$userCode, $timestamp], $config['key']);
  115. $response = self::getClient()->get(rtrim($config['balance_gateway'], '/') . '/' . $userCode, [
  116. 'query' => [
  117. 'timestamp' => $timestamp,
  118. 'sign' => $sign,
  119. ],
  120. ]);
  121. return json_decode($response->getBody()->getContents(), true) ?: [];
  122. }
  123. public static function verifyPayNotify(array $params): bool
  124. {
  125. $config = PaymentProviderConfig::snapshot('jd');
  126. if (($params['userCode'] ?? '') !== $config['mch_id']) {
  127. return false;
  128. }
  129. $sign = self::signature([
  130. $params['orderCode'] ?? '',
  131. (string)($params['amount'] ?? ''),
  132. $params['userCode'] ?? '',
  133. (string)($params['status'] ?? ''),
  134. ], $config['key']);
  135. return hash_equals($sign, strtoupper((string)($params['sign'] ?? '')));
  136. }
  137. public static function verifyRemitNotify(array $params): bool
  138. {
  139. $config = PaymentProviderConfig::snapshot('jd');
  140. if (($params['userCode'] ?? '') !== $config['mch_id']) {
  141. return false;
  142. }
  143. $sign = self::signature([
  144. $params['orderCode'] ?? '',
  145. $params['customerOrderCode'] ?? '',
  146. (string)($params['amount'] ?? ''),
  147. $params['userCode'] ?? '',
  148. (string)($params['status'] ?? ''),
  149. ], $config['key']);
  150. return hash_equals($sign, strtoupper((string)($params['sign'] ?? '')));
  151. }
  152. private static function post(string $url, array $data): array
  153. {
  154. $response = self::getClient()->post($url, [
  155. 'form_params' => $data,
  156. 'headers' => [
  157. 'Content-Type' => 'application/x-www-form-urlencoded',
  158. ],
  159. ]);
  160. return json_decode($response->getBody()->getContents(), true) ?: [];
  161. }
  162. public static function getWhere(array $search = []): array
  163. {
  164. return [];
  165. }
  166. }