AirwallexPay.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\common\library;
  3. class AirwallexPay
  4. {
  5. private $client_id = 'WUeckl4-Q-qxQXOda3uzTw';
  6. private $api_key = 'acfb93ca8d03524321a2b2b688ea8676d9f22ebaacde4bf11e0da28a50b719d8b312a38e7bdbfd97b676ce6f395ba3b7';
  7. private $webhooks_key = 'whsec_zuNFByhGiZ4l0JZWTcKtdpxeeprd0nlv';
  8. private $url = 'https://api.airwallex.com/';
  9. //测试配置
  10. // private $client_id = 'BDgsMC2fQjWg9rvl7XmaTg';
  11. // private $api_key = '635f574fc5e9c30a0edbb1707ff2ac345bba4d9e1ce9dc49dc36112b528cefe418ccb3e66a31fe9eb07bf03b88f4a3d6';
  12. // private $webhooks_key = 'whsec_6bygt6gr-EWPBZIg8qFj2KkrqY1ZzWRV';
  13. // private $url = 'https://api-demo.airwallex.com/';
  14. public function getToken()
  15. {
  16. // if(!cache('AirwallexPay:getToken')){
  17. $action = 'api/v1/authentication/login';
  18. $header = [
  19. 'Content-Type:application/json',
  20. 'x-client-id:' . $this->client_id,
  21. 'x-api-key:' . $this->api_key,
  22. 'Content-Length: ' . strlen('')
  23. ];
  24. log_message('info:'.$this->url . $action .' '.json_encode($header), 'log', LOG_PATH . 'PayNotify/airwallex/pay/');
  25. $result = curlHeader($this->url . $action, [], $header);
  26. log_message('info:'.$result, 'log', LOG_PATH . 'PayNotify/airwallex/pay/');
  27. $result = json_decode($result,true);
  28. if(isset($result['token']) && $result['token']){
  29. cache('AirwallexPay:getToken',$result['token'],25*60);
  30. return true;
  31. // }else{
  32. // return false;
  33. // }
  34. }
  35. return true;
  36. }
  37. public function createOrderUrl(){
  38. $token = cache('AirwallexPay:getToken');
  39. $action = 'api/v1/pa/payment_links/create';
  40. $data = [
  41. 'amount' => 0.01,
  42. 'currency'=>'CNY',
  43. // 'expires_at' => date('c',time()+30*60), //到期时间
  44. 'reusable' => true,//链接是否可重复使用
  45. 'title'=>'测试',
  46. ];
  47. $param = json_encode($data);
  48. $header = [
  49. 'Content-Type:application/json',
  50. 'Authorization: ' . $token,
  51. 'Content-Length: ' . strlen($param)
  52. ];
  53. $result = curlHeader($this->url . $action, $param, $header);
  54. echo $result;
  55. $result = json_decode($result,true);
  56. }
  57. public function createOrder($amount,$orderId,$return_url)
  58. {
  59. $token = cache('AirwallexPay:getToken');
  60. $action = 'api/v1/pa/payment_intents/create';
  61. $data = [
  62. 'amount' =>$amount,
  63. 'currency' => 'USD',
  64. 'merchant_order_id' => $orderId, //订单号
  65. 'request_id' => makeOrderid('AIRWA'),//唯一请求id
  66. 'payment_method_options' => [
  67. 'card' => [
  68. 'risk_control' => [
  69. 'skip_risk_processing' => false,
  70. 'three_domain_secure_action' => 'FORCE_3DS',
  71. 'three_ds_action' => 'FORCE_3DS',
  72. ],
  73. 'three_ds_action' => 'FORCE_3DS',
  74. ],
  75. ],
  76. 'return_url'=>$return_url,
  77. ];
  78. $param = json_encode($data);
  79. $header = [
  80. 'Content-Type:application/json',
  81. 'Authorization: ' . $token,
  82. 'Content-Length: ' . strlen($param)
  83. ];
  84. log_message('info:'.$this->url . $action.' '.$param .' '.json_encode($header), 'log', LOG_PATH . 'PayNotify/airwallex/pay/');
  85. $result = curlHeader($this->url . $action, $param, $header,true);
  86. log_message('info:'.json_encode($result), 'log', LOG_PATH . 'PayNotify/airwallex/pay/');
  87. if($result['code_qf'] == 201){
  88. return [
  89. 'id' =>$result['id'],
  90. 'currency' =>$result['currency'],
  91. 'client_secret' =>$result['client_secret'],
  92. 'amount'=>$result['amount'],
  93. ];
  94. }else{
  95. return [];
  96. }
  97. }
  98. public function notify($json,$header){
  99. if (hash_hmac('sha256', $header['timestamp'] . $json, $this->webhooks_key) != $header['signature']) {
  100. throw new \Exception('signature invalid');
  101. }
  102. //状态处理
  103. $data = json_decode($json, true);
  104. if (empty($data)) {
  105. throw new \Exception('data error');
  106. }
  107. if($data['name'] == 'payment_intent.succeeded' && $data['data']['object']['status'] == 'SUCCEEDED'){ //支付处理成功
  108. return true;
  109. }
  110. return false;
  111. }
  112. }