class ApplePayController extends ApiBaseController
{
const SANDBOX_URL = 'https://sandbox.itunes.apple.com/verifyReceipt'; //测试环境 验证请求
const BUY_URL = 'https://buy.itunes.apple.com/verifyReceipt'; //正式环境 验证请求
/**
* 通过CURL发送HTTP请求
* @param string $url //请求URL
* @param array $postFields //请求参数
* @return mixed
*/
public static function curlPost($postFields, $sandbox = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = $sandbox?(self::SANDBOX_URL):(self::BUY_URL);
// $url = self::BUY_URL;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // ssl 访问核心参数
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ssl 访问核心参数
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* 生成订单
*/
public function actionCreateOrder() {
$requestParams = \Yii::$app->request->post();
$validator = new ApplePayValidator($requestParams);
if(!$validator->check()) {
throw new ApplePayException($validator->getLastError());
}
$arr = ['MEMBER','ACTIVATE_PRESIDENT','PRESIDENT'];
if(in_array($requestParams['goods_type'],$arr)) {
throw new ApplePayException('维护中,请使用智慧币开通!');
}
//创建订单
$id = ApplePayService::createOrder($this->getUserId(),$requestParams['goods_type'],$requestParams['product_id']);
return $this->response(0,$this->successMsg,$id);
}
/**
* 生成订单
*/
public function actionCreateOrder2() {
$requestParams = \Yii::$app->request->post();
$validator = new ApplePayValidator($requestParams);
if(!$validator->check()) {
throw new ApplePayException($validator->getLastError());
}
$userRoles = UserRoleService::getUserRoles($this->getUserId());
if($requestParams['goods_type'] == 'MEMBER') {
if(in_array(8,$userRoles)){
throw new UsersException('您现在是股东,不需要开通会员/会长身份!');
}
if(in_array(3,$userRoles)){
//获取会长身份是否是永久的
$flag = UserRoleService::getRoleAll($this->getUserId(),3);
if($flag) {
throw new UsersException('您现在是会长,不需要开通会员身份!');
}
}
}
if($requestParams['goods_type'] == 'PRESIDENT') {
throw new UsersException('iOS暂不支持购买会长!');
if(in_array(8,$userRoles)){
throw new UsersException('您现在是股东,不需要开通会长身份!');
}
}
//创建订单
$id = ApplePayService::createOrder($this->getUserId(),$requestParams['goods_type'],$requestParams['product_id']);
return $this->response(0,$this->successMsg,$id);
}
/**
* 更改订单
* 订单ID
* 支付状态
* 第三方订单ID
*/
public function actionUpdateOrder() {
$requestParams = \Yii::$app->request->post();
if(empty($requestParams['order_id'])) {
throw new Exception('订单ID不能为空!');
}
if(empty($requestParams['status'])){
throw new Exception('订单支付状态不能为空!');
}
// if(empty($requestParams['trade_no'])) {
// throw new Exception('第三方付支付流水号不能为空!');
// }
$order = MeOrder::find()->where('id='.$requestParams['order_id'])->one();
if(!$order || $order['status'] <> 0) {
throw new Exception('订单异常');
}
//支付状态
// $order->status = $requestParams['status'];
try {
if($requestParams['status'] == 9) {
$order->pay_time = date('Y-m-d H:i:s');
$order->trade_no = $requestParams['trade_no'];
$res = $order->save();
if($res){
UserRoleService::setDeviceType(1);
PayBaseService::backCallHandle($requestParams['trade_no'],PayStatus::SUCCESS);
}
}else {
$order->pay_time = date('Y-m-d H:i:s');
$order->trade_no = $requestParams['trade_no'];
$order->status = $requestParams['status'];
$res = $order->save();
}
}catch (Exception $e) {
DebugService::debug(__FILE__, __LINE__, __CLASS__ ,__METHOD__, 'applepay_v2', [
'message' => $e->getMessage()
]);
throw new Exception($e->getMessage());
}
return $this->response(0,$this->successMsg,$res);
}
/**
*
*/
public function actionUpdateOrder2() {
$requestParams = \Yii::$app->request->post();
if(empty($requestParams['order_id'])) {
throw new Exception('订单ID不能为空!');
}
if(empty($requestParams['status'])){
throw new Exception('订单支付状态不能为空!');
}
if(empty($requestParams['trade_no'])){
throw new Exception('Apple订单号不能为空!');
}
if(empty($requestParams['receipt_data'])){
throw new Exception('apple凭据不能为空!');
}
$order = MeOrder::find()->where('id='.$requestParams['order_id'])->one();
// if(!$order || $order['status'] <> 0) {
// throw new Exception('订单异常');
// }
try {
if($requestParams['status'] == 9) {
$data = [];
//获取凭据
//ios会得到一个字节流,然后 base64
//receiptData: 客户端返回的base64
$data['receipt-data'] = $requestParams['receipt_data'];
//验证凭据
$result = json_decode(self::curlPost(json_encode($data),false),true);
if($result['status'] === 21007){
//测试的凭证
$result = json_decode(self::curlPost(json_encode($data),true),true);
}
if($result['status'] !== 0){
//凭证验证失败
throw new ApyFailException();
}
//验证成功
if(empty($result['receipt']['in_app'])) {
//空数据 需要刷新通知ios刷新凭证
throw new RefreshReceiptException();
}
$trade_no = '';
foreach ($result['receipt']['in_app'] as $key => $val) {
if($val['transaction_id'] == $requestParams['trade_no']) {
$trade_no = $val['transaction_id'];
}
//先判断订单是否存在 存在则不处理
// $isExist = self::isExistOrder($val['transaction_id']);
// if ($isExist === true) {
// continue;
// }
// //更新订单
// $trade_no = $val['transaction_id'];
}
if(empty($trade_no)){
//Apple订单号在凭据中不存在,就报错并且订单修改成失败
$order->pay_time = date('Y-m-d H:i:s');
$order->trade_no = $requestParams['trade_no'];
$order->status = 10;
$order->save();
throw new ApyFailException();
}
$order->pay_time = date('Y-m-d H:i:s');
$order->trade_no = $trade_no;
$res = $order->save();
if($res){
UserRoleService::setDeviceType(1);
PayBaseService::backCallHandle($requestParams['trade_no'],PayStatus::SUCCESS);
}
}else {
$order->pay_time = date('Y-m-d H:i:s');
$order->trade_no = $requestParams['trade_no'];
$order->status = $requestParams['status'];
$res = $order->save();
}
return $this->response(0,$this->successMsg,$res);
}catch (Exception $e) {
DebugService::debug(__FILE__, __LINE__, __CLASS__ ,__METHOD__, 'applepay_v2', [
'message' => $e->getMessage()
]);
throw new Exception($e->getMessage());
}
}
/**
*
*/
public static function isExistOrder($orderNo)
{
$num = MeOrder::find()->where(['trade_no'=>$orderNo,'del_flag'=>MeOrder::DEL_FLAG_FALSE])
->count();
if($num>0){
return true;
}
return false;
}
}