1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| public function validatePay() { $param = $this->param; $receipt_data = $param['receipt_data']; Log::info('内购receipt_data_old:' . $receipt_data); $receipt_data = str_replace(" ", "+", $receipt_data); Log::info('内购receipt_data:' . $receipt_data); Log::info('内购receipt_data_json:' . json_encode($receipt_data)); $orderSn = $param['order_sn'] ?? 0; $sandbox = Env::get('APP_ENV') != 'prod'; $result = validate_apple_pay($receipt_data, $sandbox); Log::info('内购回调res:' . json_encode($result)); if ($result['status']) { $payId = $result['data']['transactionId']; $time = $result['data']['purchaseTime'] / 1000; $order = $this->orderService->getOrderBySn($orderSn); if (is_null($order)) { throw new BusinessException('无此订单'); } $isHasPaid = $this->orderService->isHasPaid($orderSn); if ($isHasPaid) { return $this->dataToJson(0, $isHasPaid); } $res = $this->orderService->payOrder($order, $payId, $time); return $this->dataToJson(0, $res);
} else { return $this->dataToJson(1, $result['message']);
}
}
public function renew() { $resp_str = $this->request->post(); Log::info('resp_str' . json_encode($resp_str)); $data = $resp_str['signedPayload']; $data = self::verifyToken($data); $data['signedTransactionInfo'] = self::verifyToken($data['data']['signedTransactionInfo']); $data['signedRenewalInfo'] = self::verifyToken($data['data']['signedRenewalInfo']); if ($data) {
$notification_type = $data['notificationType']; $transactionData = $data['signedTransactionInfo']; $product_id = $transactionData['productId']; $sub_type = isset($data['subtype']) ? $data['subtype'] : ''; $original_transaction_id = $transactionData['originalTransactionId']; $transaction_id = $transactionData['transactionId']; $expires_date = date('Y-m-d H:i:s', $transactionData['expiresDate'] / 1000); $orderinfo = $this->orderModel->where('pay_id', $original_transaction_id)->find();
if (in_array($notification_type, ['DID_RENEW', 'SUBSCRIBED'])) { $time = time(); $this->orderService->payOrder($orderinfo, $transaction_id, $time); Log::info('开通成功以及续订成功处理交易, 续订成功userid--' . $orderinfo['user_id']); }
if (in_array($notification_type, ['REFUND'])) { Log::info('用户退款, userid--' . $orderinfo['user_id']);
}
if (in_array($notification_type, ['EXPIRED', 'DID_FAIL_TO_RENEW']) || ($notification_type == 'DID_CHANGE_RENEWAL_STATUS')) { $is_renew = 0; if (($notification_type == 'DID_CHANGE_RENEWAL_STATUS') && $sub_type == 'AUTO_RENEW_ENABLED') { $time = time(); $this->orderService->payOrder($orderinfo, $transaction_id, $time); Log::info('开通订阅成功, 续订成功userid--' . $orderinfo['user_id']); } elseif (($notification_type == 'DID_CHANGE_RENEWAL_STATUS') && $sub_type == 'AUTO_RENEW_DISABLED') { Log::info('取消订阅成功,userid--' . $orderinfo['user_id']);
} elseif (($notification_type == 'EXPIRED') && $sub_type == 'VOLUNTARY') { Log::info('订阅在用户禁用订阅续订后过期,userid--' . $orderinfo['user_id']);
} }
} }
public static function verifyToken($Token) { $tokens = explode('.', $Token); if (count($tokens) != 3) return false;
list($base64header, $base64payload) = $tokens;
$base64decodeheader = json_decode(self::base64UrlDecode($base64header), true, 512, JSON_OBJECT_AS_ARRAY); if (empty($base64decodeheader['alg']) || $base64decodeheader['alg'] != 'ES256') return false;
$payload = json_decode(self::base64UrlDecode($base64payload), true, 512, JSON_OBJECT_AS_ARRAY);
return $payload; }
private static function base64UrlEncode($input) { return str_replace('=', '', strtr(base64_encode($input), '+/', '-_')); }
private static function base64UrlDecode($input) { $remainder = strlen($input) % 4; if ($remainder) { $addlen = 4 - $remainder; $input .= str_repeat('=', $addlen); } return base64_decode(strtr($input, '-_', '+/')); } }
|