AdaPayController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\api\controller;
  3. use AdaPay\AdaTools;
  4. use app\models\article\ArticleContent;
  5. use app\models\store\StoreOrder;
  6. use app\models\user\UserRecharge;
  7. use app\Request;
  8. use crmeb\services\UtilService;
  9. include_once dirname(__FILE__). "/AdapayCore/utils/AdaTools.php";
  10. /**
  11. *
  12. * Class DouYiCantroller
  13. * @package app\api\controller
  14. */
  15. class AdaPayController
  16. {
  17. public $rsaPublicKeyFilePath = "";
  18. public $rsaPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwN6xgd6Ad8v2hIIsQVnbt8a3JituR8o4Tc3B5WlcFR55bz4OMqrG/356Ur3cPbc2Fe8ArNd/0gZbC9q56Eb16JTkVNA/fye4SXznWxdyBPR7+guuJZHc/VW2fKH2lfZ2P3Tt0QkKZZoawYOGSMdIvO+WqK44updyax0ikK6JlNQIDAQAB";
  19. public function notify(Request $request)
  20. {
  21. $post = $request->post();
  22. $data = json_decode($post['data']);
  23. $sing = $post['sign'];
  24. $res = $this->verifySign($sing, $post['data']);
  25. if($res){
  26. if ($data->status == 'succeeded'){
  27. $order = strstr($data->order_no, '-', -1);
  28. $res = StoreOrder::paySuccess($order, $data->pay_channel);
  29. if ($res){
  30. echo '200';
  31. exit();
  32. }
  33. }
  34. echo 'error';
  35. exit();
  36. }else {
  37. echo 'error';
  38. exit();
  39. }
  40. }
  41. public function cz_notify(Request $request)
  42. {
  43. $post = $request->post();
  44. $data = json_decode($post['data']);
  45. $sing = $post['sign'];
  46. $res = $this->verifySign($sing, $post['data']);
  47. if($res){
  48. if ($data->status == 'succeeded'){
  49. $order = strstr($data->order_no, '-', -1);
  50. $res = UserRecharge::rechargeSuccess($order);
  51. if ($res){
  52. echo '200';
  53. exit();
  54. }
  55. }
  56. echo 'error';
  57. exit();
  58. }else {
  59. echo 'error';
  60. exit();
  61. }
  62. }
  63. public function checkEmpty($value) {
  64. if (!isset($value))
  65. return true;
  66. if ($value === null)
  67. return true;
  68. if (trim($value) === "")
  69. return true;
  70. return false;
  71. }
  72. public function verifySign($signature, $data){
  73. if($this->checkEmpty($this->rsaPublicKeyFilePath)){
  74. $pubKey=$this->rsaPublicKey;
  75. $key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($pubKey, 64, "\n", true)."\n-----END PUBLIC KEY-----";
  76. }else {
  77. $pubKey = file_get_contents($this->rsaPublicKeyFilePath);
  78. $key = openssl_get_publickey($pubKey);
  79. }
  80. if (openssl_verify($data, base64_decode($signature), $key)){
  81. return true;
  82. }else{
  83. return false;
  84. }
  85. }
  86. }