-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: develop unified-pay api Signed-off-by: xy <[email protected]> * feat: optimize unified-pay api Signed-off-by: xy <[email protected]> * feat: Translated some Chinese TODO Signed-off-by: xy <[email protected]> * fix some errors Signed-off-by: xy <[email protected]> * change encryption algorithm Signed-off-by: xy <[email protected]> * optimized code Signed-off-by: xy <[email protected]> * build gcr images Signed-off-by: xy <[email protected]> * fix bug and optimized code Signed-off-by: xy <[email protected]> * remove service/payment * fix cluster-image-build arm64 * build pay service cluter image * fix build service * fix package imports --------- Signed-off-by: xy <[email protected]> Co-authored-by: xy <[email protected]>
- Loading branch information
Showing
35 changed files
with
1,828 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM gcr.io/distroless/static:nonroot | ||
|
||
ARG TARGETARCH | ||
COPY bin/service-pay-$TARGETARCH /manager | ||
EXPOSE 2303 | ||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/manager"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Image URL to use all building/pushing image targets | ||
IMG ?= ghcr.io/labring/sealos-pay-service:latest | ||
|
||
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) | ||
ifeq (,$(shell go env GOBIN)) | ||
GOBIN=$(shell go env GOPATH)/bin | ||
else | ||
GOBIN=$(shell go env GOBIN) | ||
endif | ||
|
||
# only support linux, non cgo | ||
PLATFORMS ?= linux_arm64 linux_amd64 | ||
GOOS=linux | ||
CGO_ENABLED=0 | ||
TARGETARCH=arm64 | ||
GOARCH=$(shell go env GOARCH) | ||
|
||
GO_BUILD_FLAGS=-trimpath -ldflags "-s -w" | ||
|
||
.PHONY: all | ||
all: build | ||
|
||
##@ General | ||
|
||
# The help target prints out all targets with their descriptions organized | ||
# beneath their categories. The categories are represented by '##@' and the | ||
# target descriptions by '##'. The awk commands is responsible for reading the | ||
# entire set of makefiles included in this invocation, looking for lines of the | ||
# file as xyz: ## something, and then pretty-format the target and help. Then, | ||
# if there's a line with ##@ something, that gets pretty-printed as a category. | ||
# More info on the usage of ANSI control characters for terminal formatting: | ||
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters | ||
# More info on the awk command: | ||
# http://linuxcommand.org/lc3_adv_awk.php | ||
|
||
.PHONY: help | ||
help: ## Display this help. | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
|
||
##@ Build | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f $(SERVICE_NAME) | ||
|
||
.PHONY: build | ||
build: clean ## Build service-hub binary. | ||
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) go build $(GO_BUILD_FLAGS) -o bin/manager main.go | ||
|
||
.PHONY: docker-build | ||
docker-build: build | ||
mv bin/manager bin/service-pay-$(TARGETARCH) | ||
docker build -t $(IMG) . | ||
|
||
.PHONY: docker-push | ||
docker-push: | ||
docker push $(IMG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/pay/handler" | ||
"github.com/labring/sealos/service/pay/helper" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func GetAppDetails(c *gin.Context, client *mongo.Client) { | ||
request, err := helper.Init(c, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("init failed before get app details: %v, %v", request, err)}) | ||
return | ||
} | ||
|
||
payDetails, err := handler.GetAppDetails(request, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("get app details failed : %v", err)}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "get app details success", | ||
"payDetails": payDetails, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/pay/handler" | ||
"github.com/labring/sealos/service/pay/helper" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func GetBill(c *gin.Context, client *mongo.Client) { | ||
request, err := helper.Init(c, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("init failed before get bills: %v, %v", request, err)}) | ||
return | ||
} | ||
|
||
billDetails, err := handler.GetBillDetails(request, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("get bill details failed : %v", err)}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "get the bill details of user(" + request.User + ") success", | ||
"billDetails": billDetails, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package api | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/pay/handler" | ||
"github.com/labring/sealos/service/pay/helper" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func CreatePayApp(c *gin.Context, client *mongo.Client) { | ||
request, err := helper.Init(c, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("init failed before create payapp: %v, %v", request, err)}) | ||
return | ||
} | ||
|
||
appName := request.PayAppName | ||
// check if the app Name already exists in appcoll | ||
if err := handler.CheckAppNameExistOrNot(client, appName); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("app name already exists: %v", err)}) | ||
return | ||
} | ||
|
||
// generate sign | ||
data := []byte(appName + time.Now().Format("20060102150405")) | ||
hash := sha256.Sum256(data) | ||
sign := hex.EncodeToString(hash[:7]) | ||
// generate appID | ||
data = []byte(sign + time.Now().Format("20060102150405")) | ||
hash = sha256.Sum256(data) | ||
var appID int64 | ||
_, err = fmt.Sscanf(hex.EncodeToString(hash[:7]), "%16x", &appID) | ||
if err != nil { | ||
fmt.Println("appID could not be generated:", err) | ||
return | ||
} | ||
// TODO At present, only wechat and stripe are supported, and then you can consider extending them | ||
methods := []string{"wechat", "stripe"} | ||
|
||
result, err := handler.InsertApp(client, appID, sign, appName, methods) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("create pay app failed when insert into db: %v", err)}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "create pay app success", | ||
"payAppName": appName, | ||
"appID": appID, | ||
"sign": sign, | ||
"result": result, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/pay/handler" | ||
"github.com/labring/sealos/service/pay/helper" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func CreatePayMethod(c *gin.Context, client *mongo.Client) { | ||
request, err := helper.Init(c, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("init failed before create paymethod: %v, %v", request, err)}) | ||
return | ||
} | ||
|
||
if ok, err := handler.CheckPayMethodExistOrNot(client, request.Currency, request.PayMethod); ok && err == nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "paymethod is exist"}) | ||
return | ||
} | ||
|
||
result, err := handler.InsertPayMethod(request, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("create pay method failed when insert into db: %v", err)}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "create pay method success", | ||
"result": result, | ||
}) | ||
} | ||
|
||
// TODO Change the amount or exchange rate or tax rate for a payment method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/pay/helper" | ||
"github.com/labring/sealos/service/pay/method" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
// GetSession Get url from payment service providers (such as WeChat and Stripe) | ||
func GetSession(c *gin.Context, client *mongo.Client) { | ||
request, err := helper.Init(c, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("init failed before get paysession: %v, %v", request, err)}) | ||
return | ||
} | ||
|
||
switch request.PayMethod { | ||
case "wechat": | ||
method.GetWechatURL(c, request, client) | ||
case "stripe": | ||
method.GetStripeSession(c, request, client) | ||
default: | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("paymethod is illegal: %v", request.PayMethod)}) | ||
} | ||
// TODO At present, the Currency of wechat and stripe seems to be CNY, and then if there are other currencies, here needs to be changed | ||
// TODO To prevent multiple orders from the same IP address, you need to add IP restrictions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/pay/helper" | ||
"github.com/labring/sealos/service/pay/method" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func GetPayStatus(c *gin.Context, client *mongo.Client) { | ||
request, err := helper.Init(c, client) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("init failed before get payment status: %v, %v", request, err)}) | ||
return | ||
} | ||
|
||
switch request.PayMethod { | ||
case "wechat": | ||
method.GetWechatPaymentStatus(c, request, client) | ||
case "stripe": | ||
method.GetStripePaymentStatus(c, request, client) | ||
default: | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("paymethod is illegal: %v", request.PayMethod)}) | ||
} | ||
// TODO The other status of this area except notpaid has not been tested, and it will be tested later | ||
} |
Oops, something went wrong.