yangjian102621 / geekai

AI 助手全套开源解决方案,自带运营管理后台,开箱即用。集成了 ChatGPT, Azure, ChatGLM,讯飞星火,文心一言等多个平台的大语言模型。支持 MJ AI 绘画,Stable Diffusion AI 绘画,微博热搜等插件工具。采用 Go + Vue3 + element-plus 实现。
https://chat.geekai.me
Apache License 2.0
3.79k stars 963 forks source link

添加apple、google账户授权登录;增加paypal、Stripe支付接口。 #224

Open lhy8888 opened 4 months ago

lhy8888 commented 4 months ago

⚠️ 确认 issue 是否已存在 ⚠️

功能描述 📝

更新前端 Login.vue 文件

文件路径:

更新内容:

vue

更新后端 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 文件

文件路径:

更新内容:

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

yangjian102621 commented 4 months ago

这个不好接,主要是账号不好申请。