Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
lhy8888 opened this issue Jul 12, 2024 · 1 comment
Open
1 task done
Labels
feature New feature or request

Comments

@lhy8888
Copy link

lhy8888 commented Jul 12, 2024

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

  • 我已经搜索了现有的问题,没有找到相关 issue。

功能描述 📝

更新前端 Login.vue 文件

文件路径:

  • web/src/views/admin/Login.vue

更新内容:

vue

<script> import { GoogleLogin } from '@react-oauth/google'; import AppleLogin from '@apple/auth'; export default { components: { GoogleLogin, AppleLogin }, methods: { handleGoogleLoginSuccess(response) { // 处理 Google 登录成功 this.$axios.post('/api/auth/google', { token: response.credential }).then(/* ... */); }, handleGoogleLoginError(error) { // 处理 Google 登录错误 console.error(error); }, handleAppleLoginSuccess(response) { // 处理 Apple 登录成功 this.$axios.post('/api/auth/apple', { token: response.code }).then(/* ... */); }, handleAppleLoginError(error) { // 处理 Apple 登录错误 console.error(error); } } }; </script>

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

@lhy8888 lhy8888 added the feature New feature or request label Jul 12, 2024
@yangjian102621
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants