Skip to content

Commit

Permalink
Get payment information from payment response status (labring#3370)
Browse files Browse the repository at this point in the history
* Get payment information from payment response status

* rename status -> orderResp
  • Loading branch information
bxy4543 committed Jun 15, 2023
1 parent a76c0f9 commit 5558cac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion controllers/account/api/v1/debt_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (d DebtValidate) Handle(ctx context.Context, req admission.Request) admissi
logger.V(1).Info("pass for kube-system")
return admission.ValidationResponse(true, "")
case userSaGroup:
logger.V(1).Info("check for user", "user", req.UserInfo.Username, "ns: ", req.Namespace)
logger.V(1).Info("check for user", "user", req.UserInfo.Username, "ns: ", req.Namespace, "name: ", req.Name, "Operation", req.Operation)
if isWhiteList(req) {
return admission.ValidationResponse(true, "")
}
Expand Down
16 changes: 9 additions & 7 deletions controllers/account/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func (r *AccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, fmt.Errorf("get account failed: %v", err)
}

status, err := pay.QueryOrder(payment.Status.TradeNO)
orderResp, err := pay.QueryOrder(payment.Status.TradeNO)
if err != nil {
return ctrl.Result{}, fmt.Errorf("query order failed: %v", err)
}
r.Logger.V(1).Info("query order status", "status", status)
switch status {
r.Logger.V(1).Info("query order status", "orderResp", orderResp)
switch *orderResp.TradeState {
case pay.StatusSuccess:
dbCtx := context.Background()
dbClient, err := database.NewMongoDB(dbCtx, r.MongoDBURI)
Expand All @@ -128,12 +128,14 @@ func (r *AccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
}()
now := time.Now().UTC()
var gift = giveGift(payment.Spec.Amount)
account.Status.Balance += payment.Spec.Amount + gift
payAmount := *orderResp.Amount.Total * 10000
//1¥ = 100WechatPayAmount; 1 WechatPayAmount = 10000 SealosAmount
var gift = giveGift(payAmount)
account.Status.Balance += payAmount + gift
if err := r.Status().Update(ctx, account); err != nil {
return ctrl.Result{}, fmt.Errorf("update account failed: %v", err)
}
payment.Status.Status = status
payment.Status.Status = pay.StatusSuccess
if err := r.Status().Update(ctx, payment); err != nil {
return ctrl.Result{}, fmt.Errorf("update payment failed: %v", err)
}
Expand Down Expand Up @@ -163,7 +165,7 @@ func (r *AccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
return ctrl.Result{}, nil
default:
return ctrl.Result{}, fmt.Errorf("unknown status: %v", status)
return ctrl.Result{}, fmt.Errorf("unknown orderResp: %v", orderResp)
}

return ctrl.Result{}, nil
Expand Down
4 changes: 3 additions & 1 deletion controllers/account/controllers/payment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"time"

"sigs.k8s.io/controller-runtime/pkg/builder"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"

Expand Down Expand Up @@ -98,6 +100,6 @@ func (r *PaymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Logger = ctrl.Log.WithName(controllerName)
r.Logger.V(1).Info("init reconcile controller payment")
return ctrl.NewControllerManagedBy(mgr).
For(&accountv1.Payment{}).
For(&accountv1.Payment{}, builder.WithPredicates(OnlyCreatePredicate{})).
Complete(r)
}
10 changes: 6 additions & 4 deletions pkg/pay/wechat_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"os"
"time"

"github.com/wechatpay-apiv3/wechatpay-go/services/payments"

"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
Expand Down Expand Up @@ -57,11 +59,11 @@ func NewClient(ctx context.Context, opts ...core.ClientOption) (*core.Client, er
return core.NewClient(ctx, opts...)
}

func QueryOrder(orderID string) (string, error) {
func QueryOrder(orderID string) (*payments.Transaction, error) {
ctx := context.Background()
client, err := NewClient(context.Background())
if err != nil {
return "", fmt.Errorf("new wechat pay client err:%s", err)
return nil, fmt.Errorf("new wechat pay client err:%s", err)
}
svc := native.NativeApiService{Client: client}
resp, _, err := svc.QueryOrderByOutTradeNo(ctx,
Expand All @@ -71,9 +73,9 @@ func QueryOrder(orderID string) (string, error) {
},
)
if err != nil {
return "", fmt.Errorf("call QueryOrder err:%s", err)
return nil, fmt.Errorf("call QueryOrder err:%s", err)
}
return *resp.TradeState, nil
return resp, nil
}

// 1 ¥ = amount 100
Expand Down

0 comments on commit 5558cac

Please sign in to comment.