AlipayCoupon.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * 支付宝立减金接口封装
  4. */
  5. namespace pay;
  6. if (!defined('ALIPAY_PATHEX')) define('ALIPAY_PATHEX', BASE_DATA_PATH . '/api/aop');
  7. require_once(ALIPAY_PATHEX . '/AopClient.php');
  8. require_once(ALIPAY_PATHEX . '/AopEncrypt.php');
  9. require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustChannelvoucherSendRequest.php');
  10. require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustActivityorderQueryRequest.php');
  11. require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustChannelvoucherconfigQueryRequest.php');
  12. require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankActivitybillQueryRequest.php');
  13. require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustAccountQueryRequest.php');
  14. use aop\AopClient;
  15. use aop\AlipayUserDtbankcustChannelvoucherSendRequest;
  16. use aop\AlipayUserDtbankcustActivityorderQueryRequest;
  17. use aop\AlipayUserDtbankActivitybillQueryRequest;
  18. use aop\AlipayUserDtbankcustAccountQueryRequest;
  19. use Exception;
  20. class AlipayCoupon {
  21. const ALIPAY_SERVER_URL = "https://openapi.alipay.com";
  22. const ALIPAY_GATEWAY = "/gateway.do";
  23. const ALIPAY_SIGN_TYPE = "RSA2";
  24. const ALIPAY_FORMAT = "json";
  25. const ALIPAY_POST_CHARSET = "UTF-8";
  26. const ALIPAY_API_VERSION = "1.0";
  27. private $aop_client = NULL;
  28. private static $_instance = NULL;
  29. private function __construct(AopClient $aop){
  30. $this->aop_client = $aop;
  31. }
  32. public static function getInstance(string $app_id, string $rsa_private_key, string $rsa_public_key): AlipayCoupon
  33. {
  34. if (self::$_instance == NULL){
  35. $aop = new AopClient ();
  36. $aop->gatewayUrl = self::ALIPAY_SERVER_URL.self::ALIPAY_GATEWAY;
  37. $aop->appId = $app_id;
  38. $aop->rsaPrivateKey = $rsa_private_key;
  39. $aop->alipayrsaPublicKey = $rsa_public_key;
  40. $aop->apiVersion = self::ALIPAY_API_VERSION;
  41. $aop->signType = self::ALIPAY_SIGN_TYPE;
  42. $aop->postCharset = self::ALIPAY_POST_CHARSET;
  43. $aop->format = self::ALIPAY_FORMAT;
  44. self::$_instance = new AlipayCoupon($aop);
  45. }
  46. return self::$_instance;
  47. }
  48. private function check_voucher_key(array $params): bool
  49. {
  50. if (empty($biz_content['open_id']) && empty($biz_content['logon_id']) && empty($biz_content['phone_id'])) {
  51. return false;
  52. }
  53. if (empty($biz_content['activity_id']) || empty($biz_content['out_biz_no'])) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * 发放立减金
  60. * @param string $activity_id
  61. * @param string $out_biz_no
  62. * @param string $open_id
  63. * @param string $logon_id
  64. * @param string $phone_id
  65. * @return array
  66. */
  67. public function send_voucher(string $activity_id, string $out_biz_no, string $open_id = '', string $logon_id = '', string $phone_id = ''): array
  68. {
  69. $biz_content = [];
  70. if (!empty($open_id)){
  71. $biz_content['open_id'] = $open_id;
  72. }
  73. if (!empty($logon_id)){
  74. $biz_content['logon_id'] = $logon_id;
  75. }
  76. if (!empty($phone_id)){
  77. $biz_content['phone_id'] = $phone_id;
  78. }
  79. $biz_content['activity_id'] = $activity_id;
  80. $biz_content['out_biz_no'] = $out_biz_no;
  81. if (!$this->check_voucher_key($biz_content)){
  82. return [false, '参数错误'];
  83. }
  84. $request = new AlipayUserDtbankcustChannelvoucherSendRequest();
  85. $request->setBizContent(json_encode($biz_content));
  86. try
  87. {
  88. $resp = $this->aop_client->execute($request);
  89. if ($resp === false){
  90. return [false, '网络错误'];
  91. }
  92. }
  93. catch (Exception $e)
  94. {
  95. return [false, $e->getMessage()];
  96. }
  97. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  98. return [$resp->$responseNode, ''];
  99. }
  100. /**
  101. * 查询红包流水
  102. * @param string $activity_id
  103. * @param string $out_biz_no
  104. * @param string $open_id
  105. * @param string $logon_id
  106. * @param string $phone_id
  107. * @return array
  108. */
  109. public function query_order(string $activity_id, string $out_biz_no, string $open_id = '', string $logon_id = '', string $phone_id = ''): array
  110. {
  111. if (!empty($open_id)){
  112. $biz_content['open_id'] = $open_id;
  113. }
  114. if (!empty($logon_id)){
  115. $biz_content['logon_id'] = $logon_id;
  116. }
  117. if (!empty($phone_id)){
  118. $biz_content['phone_id'] = $phone_id;
  119. }
  120. $biz_content['activity_id'] = $activity_id;
  121. $biz_content['out_biz_no'] = $out_biz_no;
  122. if (!$this->check_voucher_key($biz_content)){
  123. return [false, '参数错误'];
  124. }
  125. $request = new AlipayUserDtbankcustActivityorderQueryRequest();
  126. $request->setBizContent(json_encode($biz_content));
  127. try
  128. {
  129. $resp = $this->aop_client->execute($request);
  130. if ($resp === false){
  131. return [false, '网络错误'];
  132. }
  133. }
  134. catch (Exception $e)
  135. {
  136. return [false, $e->getMessage()];
  137. }
  138. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  139. return [$resp->$responseNode, ''];
  140. }
  141. /**
  142. * 验证签名
  143. * @param array $params
  144. * @return bool
  145. */
  146. public function verify_sign(array $params): bool
  147. {
  148. if (!$this->aop_client->rsaCheckV1($params, null, 'RSA2')){
  149. return false;
  150. }
  151. return true;
  152. }
  153. public function on_notify(array $params): array
  154. {
  155. return [];
  156. }
  157. /**
  158. * 查询活动账单
  159. * @param string $activity_id
  160. * @return array
  161. */
  162. public function query_activity_bill(string $activity_id): array
  163. {
  164. if (empty($activity_id)){
  165. return [false, '参数错误'];
  166. }
  167. $biz_content = [
  168. 'activity_id' => $activity_id,
  169. 'bill_type' => 'ALL'
  170. ];
  171. $request = new AlipayUserDtbankActivitybillQueryRequest();
  172. $request->setBizContent(json_encode($biz_content));
  173. try
  174. {
  175. $resp = $this->aop_client->execute($request);
  176. if ($resp === false) {
  177. return [false, '网络错误'];
  178. }
  179. }
  180. catch (Exception $e)
  181. {
  182. return [false, $e->getMessage()];
  183. }
  184. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  185. return [$resp->$responseNode, ''];
  186. }
  187. /**
  188. * 数字分行用户账号查询接口
  189. * $logon_id和$phone_id二选一
  190. * @param $logon_id
  191. * @param $phone_id
  192. * @return array
  193. */
  194. public function query_account($logon_id,$phone_id): array
  195. {
  196. $biz_content = [];
  197. if (!empty($phone_id)) {
  198. $biz_content['phone_id'] = $phone_id;
  199. }
  200. if (!empty($logon_id)) {
  201. $biz_content['logon_id'] = $logon_id;
  202. }
  203. if (empty($biz_content)) {
  204. return [false, '参数错误'];
  205. }
  206. $request = new AlipayUserDtbankcustAccountQueryRequest();
  207. $request->setBizContent(json_encode($biz_content));
  208. try
  209. {
  210. $resp = $this->aop_client->execute($request);
  211. if ($resp === false) {
  212. return [false, '网络错误'];
  213. }
  214. }
  215. catch (Exception $e)
  216. {
  217. return [false, $e->getMessage()];
  218. }
  219. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  220. return [$resp->$responseNode, ''];
  221. }
  222. }
  223. ?>