添加apple、google账户授权登录;增加paypal、Stripe支付接口。
lhy8888 opened this issue · comments
⚠️ 确认 issue 是否已存在 ⚠️
- 我已经搜索了现有的问题,没有找到相关 issue。
功能描述 📝
更新前端 Login.vue
文件
文件路径:
web/src/views/admin/Login.vue
更新内容:
vue
更新后端 auth_handler.go
文件
文件路径:
api/handler/auth_handler.go
更新内容:
package handler
import (
"context"
"net/http"
"github.com/dgrijalva/jwt-go"
"google.golang.org/api/oauth2/v2"
)
var googleClientID = "YOUR_GOOGLE_CLIENT_ID"
var appleClientID = "YOUR_APPLE_CLIENT_ID"
var appleTeamID = "YOUR_APPLE_TEAM_ID"
var appleKeyID = "YOUR_APPLE_KEY_ID"
var applePrivateKey = YOUR_APPLE_PRIVATE_KEY
func GoogleAuthHandler(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
oauth2Service, err := oauth2.NewService(context.Background())
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
tokenInfo, err := oauth2Service.Tokeninfo().IdToken(token).Do()
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if tokenInfo.Audience != googleClientID {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// 登录成功,处理用户信息
}
func AppleAuthHandler(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
claims := jwt.MapClaims{}
tokenParsed, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(applePrivateKey), nil
})
if err != nil || !tokenParsed.Valid {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// 验证客户端ID和其他信息
if claims["aud"] != appleClientID || claims["iss"] != appleTeamID {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// 登录成功,处理用户信息
}
更新后端 payment_handler.go
文件
文件路径:
api/handler/payment_handler.go
更新内容:
package handler
import (
"net/http"
"os"
"encoding/json"
"github.com/plutov/paypal/v4"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/paymentintent"
)
var paypalClientID = "YOUR_PAYPAL_CLIENT_ID"
var paypalSecret = "YOUR_PAYPAL_SECRET"
var stripeSecretKey = "YOUR_STRIPE_SECRET_KEY"
func PayPalSuccessHandler(w http.ResponseWriter, r *http.Request) {
client, err := paypal.NewClient(paypalClientID, paypalSecret, paypal.APIBaseSandBox)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
client.SetLog(os.Stdout) // 打开日志记录
var result paypal.ExecutePaymentResponse
err = json.NewDecoder(r.Body).Decode(&result)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// 验证支付结果
if result.State != "approved" {
http.Error(w, "Payment Not Approved", http.StatusBadRequest)
return
}
// 支付成功,处理订单逻辑
}
func StripeSuccessHandler(w http.ResponseWriter, r *http.Request) {
stripe.Key = stripeSecretKey
var result struct {
PaymentIntentID string json:"paymentIntentId"
}
err := json.NewDecoder(r.Body).Decode(&result)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
paymentIntent, err := paymentintent.Get(result.PaymentIntentID, nil)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if paymentIntent.Status != stripe.PaymentIntentStatusSucceeded {
http.Error(w, "Payment Not Successful", http.StatusBadRequest)
return
}
// 支付成功,处理订单逻辑
}
示例 🌈
No response
动机 🔦
No response
这个不好接,主要是账号不好申请。