aop_client = $aop; } public static function getInstance(string $app_id, string $rsa_private_key, string $rsa_public_key): AlipayCoupon { if (self::$_instance == NULL){ $aop = new AopClient (); $aop->gatewayUrl = self::ALIPAY_SERVER_URL.self::ALIPAY_GATEWAY; $aop->appId = $app_id; $aop->rsaPrivateKey = $rsa_private_key; $aop->alipayrsaPublicKey = $rsa_public_key; $aop->apiVersion = self::ALIPAY_API_VERSION; $aop->signType = self::ALIPAY_SIGN_TYPE; $aop->postCharset = self::ALIPAY_POST_CHARSET; $aop->format = self::ALIPAY_FORMAT; self::$_instance = new AlipayCoupon($aop); } return self::$_instance; } private function check_voucher_key(array $params): bool { if (empty($biz_content['open_id']) && empty($biz_content['logon_id']) && empty($biz_content['phone_id'])) { return false; } if (empty($biz_content['activity_id']) || empty($biz_content['out_biz_no'])) { return false; } return true; } /** * 发放立减金 * @param string $activity_id * @param string $out_biz_no * @param string $open_id * @param string $logon_id * @param string $phone_id * @return array */ public function send_voucher(string $activity_id, string $out_biz_no, string $open_id = '', string $logon_id = '', string $phone_id = ''): array { $biz_content = []; if (!empty($open_id)){ $biz_content['open_id'] = $open_id; } if (!empty($logon_id)){ $biz_content['logon_id'] = $logon_id; } if (!empty($phone_id)){ $biz_content['phone_id'] = $phone_id; } $biz_content['activity_id'] = $activity_id; $biz_content['out_biz_no'] = $out_biz_no; if (!$this->check_voucher_key($biz_content)){ return [false, '参数错误']; } $request = new AlipayUserDtbankcustChannelvoucherSendRequest(); $request->setBizContent(json_encode($biz_content)); try { $resp = $this->aop_client->execute($request); if ($resp === false){ return [false, '网络错误']; } } catch (Exception $e) { return [false, $e->getMessage()]; } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return [$resp->$responseNode, '']; } /** * 查询红包流水 * @param string $activity_id * @param string $out_biz_no * @param string $open_id * @param string $logon_id * @param string $phone_id * @return array */ public function query_order(string $activity_id, string $out_biz_no, string $open_id = '', string $logon_id = '', string $phone_id = ''): array { if (!empty($open_id)){ $biz_content['open_id'] = $open_id; } if (!empty($logon_id)){ $biz_content['logon_id'] = $logon_id; } if (!empty($phone_id)){ $biz_content['phone_id'] = $phone_id; } $biz_content['activity_id'] = $activity_id; $biz_content['out_biz_no'] = $out_biz_no; if (!$this->check_voucher_key($biz_content)){ return [false, '参数错误']; } $request = new AlipayUserDtbankcustActivityorderQueryRequest(); $request->setBizContent(json_encode($biz_content)); try { $resp = $this->aop_client->execute($request); if ($resp === false){ return [false, '网络错误']; } } catch (Exception $e) { return [false, $e->getMessage()]; } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return [$resp->$responseNode, '']; } /** * 验证签名 * @param array $params * @return bool */ public function verify_sign(array $params): bool { if (!$this->aop_client->rsaCheckV1($params, null, 'RSA2')){ return false; } return true; } public function on_notify(array $params): array { return []; } /** * 查询活动账单 * @param string $activity_id * @return array */ public function query_activity_bill(string $activity_id): array { if (empty($activity_id)){ return [false, '参数错误']; } $biz_content = [ 'activity_id' => $activity_id, 'bill_type' => 'ALL' ]; $request = new AlipayUserDtbankActivitybillQueryRequest(); $request->setBizContent(json_encode($biz_content)); try { $resp = $this->aop_client->execute($request); if ($resp === false) { return [false, '网络错误']; } } catch (Exception $e) { return [false, $e->getMessage()]; } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return [$resp->$responseNode, '']; } /** * 数字分行用户账号查询接口 * $logon_id和$phone_id二选一 * @param $logon_id * @param $phone_id * @return array */ public function query_account($logon_id,$phone_id): array { $biz_content = []; if (!empty($phone_id)) { $biz_content['phone_id'] = $phone_id; } if (!empty($logon_id)) { $biz_content['logon_id'] = $logon_id; } if (empty($biz_content)) { return [false, '参数错误']; } $request = new AlipayUserDtbankcustAccountQueryRequest(); $request->setBizContent(json_encode($biz_content)); try { $resp = $this->aop_client->execute($request); if ($resp === false) { return [false, '网络错误']; } } catch (Exception $e) { return [false, $e->getMessage()]; } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return [$resp->$responseNode, '']; } } ?>