KakaoPayService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Services\Payment;
  3. use App\Services\CallApiService;
  4. use Exception;
  5. class KakaoPayService
  6. {
  7. private $callApiService;
  8. public function __construct(CallApiService $callApiService)
  9. {
  10. $this->callApiService = $callApiService;
  11. }
  12. public function ready($agent, $form)
  13. {
  14. $properties = config('kakaopay.properties');
  15. $form_params = [];
  16. $form_params["cid"] = $properties['cid']; // 가맹점 코드
  17. $form_params["approval_url"] = env('APP_URL')."/kakaopay-approve/$agent/redirect"; // 결제성공 redirect url
  18. // $form_params["cancel_url"] = env('APP_URL')."/kakaopay-approve/$agent/redirect"; // 결제성공 redirect url
  19. $form_params["cancel_url"] = env('APP_URL')."/checkout-cancel"; // 결제취소 redirect url
  20. $form_params["fail_url"] = env('APP_URL')."/checkout-failed"; // 결제실패 redirect url
  21. $form_params = array_merge($form_params, $form);
  22. $body = \Unirest\Request\Body::form($form_params);
  23. $response = \Unirest\Request::post(
  24. 'https://kapi.kakao.com/v1/payment/ready',
  25. [
  26. 'Authorization' => 'KakaoAK '.$properties['kakao_api_admin_key'],
  27. 'Content-Type' => 'application/x-www-form-urlencoded',
  28. ],
  29. $body
  30. );
  31. $data = json_encode($response->body ?? []);
  32. $data = json_decode($data, true);
  33. return $data;
  34. }
  35. public function approve($pg_token, $tid)
  36. {
  37. $properties = config('kakaopay.properties');
  38. $form_params = [];
  39. $form_params["cid"] = $properties['cid']; // 가맹점 코드
  40. $form_params["tid"] = $tid; // 결제 고유번호
  41. $form_params["partner_order_id"] = "test12345"; // 주문번호(ready할 때 사용했던 값)
  42. $form_params["partner_user_id"] = "1"; // 회원 ID(ready할 때 사용했던 값)
  43. $form_params["pg_token"] = $pg_token; // pg token
  44. try {
  45. $body = \Unirest\Request\Body::form($form_params);
  46. $response = \Unirest\Request::post(
  47. 'https://kapi.kakao.com/v1/payment/approve',
  48. [
  49. 'Authorization' => 'KakaoAK '.$properties['kakao_api_admin_key'],
  50. 'Content-Type' => 'application/x-www-form-urlencoded',
  51. ],
  52. $body
  53. );
  54. $data = json_encode($response->body ?? []);
  55. return json_decode($data, true);
  56. } catch (Exception $e) {
  57. return $e;
  58. }
  59. }
  60. }