|
| 1 | +package paypal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "github.com/go-pay/gopay" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// CreateWebhook 创建Webhook |
| 13 | +func (c *Client) CreateWebhook(ctx context.Context, bm gopay.BodyMap) (ppRsp *CreateWebhookRsp, err error) { |
| 14 | + if err = bm.CheckEmptyError("url", "event_types"); nil != err { |
| 15 | + return nil, err |
| 16 | + } |
| 17 | + res, bs, err := c.doPayPalPost(ctx, bm, createWebhook) |
| 18 | + if nil != err { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + ppRsp = &CreateWebhookRsp{Code: Success} |
| 22 | + ppRsp.Response = new(Webhook) |
| 23 | + if err = json.Unmarshal(bs, &ppRsp.Response); nil != err { |
| 24 | + return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) |
| 25 | + } |
| 26 | + if res.StatusCode != http.StatusCreated { |
| 27 | + ppRsp.Code = res.StatusCode |
| 28 | + ppRsp.Error = string(bs) |
| 29 | + ppRsp.ErrorResponse = new(ErrorResponse) |
| 30 | + _ = json.Unmarshal(bs, ppRsp.ErrorResponse) |
| 31 | + } |
| 32 | + return ppRsp, nil |
| 33 | +} |
| 34 | + |
| 35 | +// ListWebhook 查询Webhook列表 |
| 36 | +func (c *Client) ListWebhook(ctx context.Context) (ppRsp *ListWebhookRsp, err error) { |
| 37 | + res, bs, err := c.doPayPalGet(ctx, listWebhook) |
| 38 | + if nil != err { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + ppRsp = &ListWebhookRsp{Code: Success} |
| 42 | + if err = json.Unmarshal(bs, &ppRsp.Response); nil != err { |
| 43 | + return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) |
| 44 | + } |
| 45 | + if res.StatusCode != http.StatusOK { |
| 46 | + ppRsp.Code = res.StatusCode |
| 47 | + ppRsp.Error = string(bs) |
| 48 | + ppRsp.ErrorResponse = new(ErrorResponse) |
| 49 | + _ = json.Unmarshal(bs, ppRsp.ErrorResponse) |
| 50 | + } |
| 51 | + return ppRsp, nil |
| 52 | +} |
| 53 | + |
| 54 | +// ShowWebhookDetail 查询Webhook |
| 55 | +func (c *Client) ShowWebhookDetail(ctx context.Context, webhookId string) (ppRsp *WebhookDetailRsp, err error) { |
| 56 | + url := fmt.Sprintf(showWebhookDetail, webhookId) |
| 57 | + res, bs, err := c.doPayPalGet(ctx, url) |
| 58 | + if nil != err { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + ppRsp = &WebhookDetailRsp{Code: Success} |
| 62 | + if err = json.Unmarshal(bs, &ppRsp.Response); nil != err { |
| 63 | + return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) |
| 64 | + } |
| 65 | + if res.StatusCode != http.StatusOK { |
| 66 | + ppRsp.Code = res.StatusCode |
| 67 | + ppRsp.Error = string(bs) |
| 68 | + ppRsp.ErrorResponse = new(ErrorResponse) |
| 69 | + _ = json.Unmarshal(bs, ppRsp.ErrorResponse) |
| 70 | + } |
| 71 | + return ppRsp, nil |
| 72 | +} |
| 73 | + |
| 74 | +// UpdateWebhook 更新Webhook消息 |
| 75 | +func (c *Client) UpdateWebhook(ctx context.Context, webhookId string, patchs []*Patch) (ppRsp *WebhookDetailRsp, err error) { |
| 76 | + url := fmt.Sprintf(updateWebhook, webhookId) |
| 77 | + res, bs, err := c.doPayPalPatch(ctx, patchs, url) |
| 78 | + if nil != err { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + ppRsp = &WebhookDetailRsp{Code: Success} |
| 82 | + if err = json.Unmarshal(bs, &ppRsp.Response); nil != err { |
| 83 | + return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) |
| 84 | + } |
| 85 | + if res.StatusCode != http.StatusOK { |
| 86 | + ppRsp.Code = res.StatusCode |
| 87 | + ppRsp.Error = string(bs) |
| 88 | + ppRsp.ErrorResponse = new(ErrorResponse) |
| 89 | + _ = json.Unmarshal(bs, ppRsp.ErrorResponse) |
| 90 | + } |
| 91 | + return ppRsp, nil |
| 92 | +} |
| 93 | + |
| 94 | +// DeleteWebhook 删除Webhook消息 |
| 95 | +func (c *Client) DeleteWebhook(ctx context.Context, webhookId string) (ppRsp *WebhookDetailRsp, err error) { |
| 96 | + url := fmt.Sprintf(deleteWebhook, webhookId) |
| 97 | + res, bs, err := c.doPayPalDelete(ctx, url) |
| 98 | + if nil != err { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + ppRsp = &WebhookDetailRsp{Code: Success} |
| 102 | + if res.StatusCode != http.StatusNoContent { |
| 103 | + ppRsp.Code = res.StatusCode |
| 104 | + ppRsp.Error = string(bs) |
| 105 | + ppRsp.ErrorResponse = new(ErrorResponse) |
| 106 | + _ = json.Unmarshal(bs, ppRsp.ErrorResponse) |
| 107 | + } |
| 108 | + return ppRsp, nil |
| 109 | +} |
| 110 | + |
| 111 | +// VerifyWebhookSignature 验证Webhook签名 |
| 112 | +// 文档:https://developer.paypal.com/docs/api/webhooks/v1/#verify-webhook-signature_post |
| 113 | +func (c *Client) VerifyWebhookSignature(ctx context.Context, bm gopay.BodyMap) (verifyRes *VerifyWebhookResponse, err error) { |
| 114 | + if err = bm.CheckEmptyError("auth_algo", "cert_url", "transmission_id", "transmission_sig", "transmission_time", "webhook_id", "webhook_event"); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + res, bs, err := c.doPayPalPost(ctx, bm, verifyWebhookSignature) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + if res.StatusCode != http.StatusOK { |
| 122 | + return verifyRes, errors.New("request paypal url[verify-webhook-signature_post] error") |
| 123 | + } |
| 124 | + verifyRes = &VerifyWebhookResponse{} |
| 125 | + if err = json.Unmarshal(bs, verifyRes); err != nil { |
| 126 | + return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) |
| 127 | + } |
| 128 | + return verifyRes, nil |
| 129 | +} |
| 130 | + |
| 131 | +// ShowWebhookEventDetail 查询Webhook-event消息 |
| 132 | +func (c *Client) ShowWebhookEventDetail(ctx context.Context, eventId string) (ppRsp *WebhookEventDetailRsp, err error) { |
| 133 | + url := fmt.Sprintf(showWebhookEventDetail, eventId) |
| 134 | + res, bs, err := c.doPayPalGet(ctx, url) |
| 135 | + if nil != err { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + ppRsp = &WebhookEventDetailRsp{Code: Success} |
| 139 | + if err = json.Unmarshal(bs, &ppRsp.Response); nil != err { |
| 140 | + return nil, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err) |
| 141 | + } |
| 142 | + if res.StatusCode != http.StatusOK { |
| 143 | + ppRsp.Code = res.StatusCode |
| 144 | + ppRsp.Error = string(bs) |
| 145 | + ppRsp.ErrorResponse = new(ErrorResponse) |
| 146 | + _ = json.Unmarshal(bs, ppRsp.ErrorResponse) |
| 147 | + } |
| 148 | + return ppRsp, nil |
| 149 | +} |
0 commit comments