Skip to content

Commit

Permalink
Add endpoint for Shopify Payments transactions && Order Risk (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlBaraa-mohamed authored Jan 8, 2024
1 parent e40406f commit 6898174
Show file tree
Hide file tree
Showing 9 changed files with 996 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fixtures/order_risk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"risk": {
"id": 284138680,
"order_id": 450789469,
"checkout_id": 0,
"source": "External",
"score": "1.0",
"recommendation": "cancel",
"display": true,
"cause_cancel": true,
"message": "This order was placed from a proxy IP",
"merchant_message": "This order was placed from a proxy IP"
}
}
28 changes: 28 additions & 0 deletions fixtures/order_risks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"risks": [
{
"id": 284138680,
"order_id": 450789469,
"checkout_id": 0,
"source": "External",
"score": "1.0",
"recommendation": "cancel",
"display": true,
"cause_cancel": true,
"message": "This order was placed from a proxy IP",
"merchant_message": "This order was placed from a proxy IP"
},
{
"id": 1029151489,
"order_id": 450789469,
"checkout_id": 901414060,
"source": "External",
"score": "1.0",
"recommendation": "cancel",
"display": true,
"cause_cancel": true,
"message": "This order came from an anonymous proxy",
"merchant_message": "This order came from an anonymous proxy"
}
]
}
18 changes: 18 additions & 0 deletions fixtures/payments_transaction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"transaction": {
"id": 699519475,
"type": "debit",
"test": false,
"payout_id": 623721858,
"payout_status": "paid",
"currency": "USD",
"amount": "-50.00",
"fee": "0.00",
"net": "-50.00",
"source_id": 460709370,
"source_type": "adjustment",
"source_order_id": 0,
"source_order_transaction_id": 0,
"processed_at": "2013-11-01"
}
}
52 changes: 52 additions & 0 deletions fixtures/payments_transactions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"transactions": [
{
"id": 699519475,
"type": "debit",
"test": false,
"payout_id": 623721858,
"payout_status": "paid",
"currency": "USD",
"amount": "-50.00",
"fee": "0.00",
"net": "-50.00",
"source_id": 460709370,
"source_type": "adjustment",
"source_order_id": null,
"source_order_transaction_id": null,
"processed_at": "2013-11-01"
},
{
"id": 77412310,
"type": "credit",
"test": false,
"payout_id": 623721858,
"payout_status": "paid",
"currency": "USD",
"amount": "50.00",
"fee": "0.00",
"net": "50.00",
"source_id": 374511569,
"source_type": "Payments::Balance::AdjustmentReversal",
"source_order_id": null,
"source_order_transaction_id": null,
"processed_at": "2013-11-01"
},
{
"id": 1006917261,
"type": "refund",
"test": false,
"payout_id": 623721858,
"payout_status": "paid",
"currency": "USD",
"amount": "-3.45",
"fee": "0.00",
"net": "-3.45",
"source_id": 1006917261,
"source_type": "Payments::Refund",
"source_order_id": 217130470,
"source_order_transaction_id": 1006917261,
"processed_at": "2013-11-01"
}
]
}
4 changes: 4 additions & 0 deletions goshopify.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type Client struct {
AssignedFulfillmentOrder AssignedFulfillmentOrderService
FulfillmentEvent FulfillmentEventService
FulfillmentRequest FulfillmentRequestService
PaymentsTransactions PaymentsTransactionsService
OrderRisk OrderRiskService
}

// A general response error that follows a similar layout to Shopify's response
Expand Down Expand Up @@ -314,6 +316,8 @@ func NewClient(app App, shopName, token string, opts ...Option) *Client {
c.AssignedFulfillmentOrder = &AssignedFulfillmentOrderServiceOp{client: c}
c.FulfillmentEvent = &FulfillmentEventServiceOp{client: c}
c.FulfillmentRequest = &FulfillmentRequestServiceOp{client: c}
c.PaymentsTransactions = &PaymentsTransactionsServiceOp{client: c}
c.OrderRisk = &OrderRiskServiceOp{client: c}

// apply any options
for _, opt := range opts {
Expand Down
122 changes: 122 additions & 0 deletions order_risk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package goshopify

import (
"fmt"
)

const ordersRiskBasePath = "orders"
const ordersRiskResourceName = "risks"

// OrderRiskService is an interface for interfacing with the orders Risk endpoints of
// the Shopify API.
// See: https://shopify.dev/docs/api/admin-rest/2023-10/resources/order-risk
type OrderRiskService interface {
List(int64, interface{}) ([]OrderRisk, error)
ListWithPagination(int64, interface{}) ([]OrderRisk, *Pagination, error)
Get(int64, int64, interface{}) (*OrderRisk, error)
Create(int64, OrderRisk) (*OrderRisk, error)
Update(int64, int64, OrderRisk) (*OrderRisk, error)
Delete(int64, int64) error
}

// OrderRiskServiceOp handles communication with the order related methods of the
// Shopify API.
type OrderRiskServiceOp struct {
client *Client
}

// Represents the result from the orders-risk/X.json endpoint
type OrderRiskResource struct {
OrderRisk *OrderRisk `json:"risk"`
}

// Represents the result from the orders-risk.json endpoint
type OrdersRisksResource struct {
OrderRisk []OrderRisk `json:"risks"`
}
type orderRiskRecommendation string

const (
//order is fraudulent.
OrderRecommendationCancel orderRiskRecommendation = "cancel"

//medium level of risk that this order is fraudulent.
OrderRecommendationInvestigate orderRiskRecommendation = "investigate"

//level of risk that this order is fraudulent.
OrderRecommendationAccept orderRiskRecommendation = "accept"
)

// A struct for all available order Risk list options.
// See: https://shopify.dev/docs/api/admin-rest/2023-10/resources/order-risk#index
type OrderRiskListOptions struct {
ListOptions
}

// OrderRisk represents a Shopify order risk
type OrderRisk struct {
Id int64 `json:"id,omitempty"`
CheckoutId int64 `json:"checkout_id,omitempty"`
OrderId int64 `json:"order_id,omitempty"`
CauseCancel bool `json:"cause_cancel,omitempty"`
Display bool `json:"display,omitempty"`
MerchantMessage string `json:"merchant_message,omitempty"`
Message string `json:"message,omitempty"`
Score string `json:"score,omitempty"`
Source string `json:"source,omitempty"`
Recommendation orderRiskRecommendation `json:"recommendation,omitempty"`
}

// List OrderRisk
func (s *OrderRiskServiceOp) List(orderId int64, options interface{}) ([]OrderRisk, error) {
orders, _, err := s.ListWithPagination(orderId, options)
if err != nil {
return nil, err
}
return orders, nil
}

func (s *OrderRiskServiceOp) ListWithPagination(orderId int64, options interface{}) ([]OrderRisk, *Pagination, error) {
path := fmt.Sprintf("%s/%d/%s.json", ordersRiskBasePath, orderId, ordersRiskResourceName)
resource := new(OrdersRisksResource)

pagination, err := s.client.ListWithPagination(path, resource, options)
if err != nil {
return nil, nil, err
}

return resource.OrderRisk, pagination, nil
}

// Get individual order
func (s *OrderRiskServiceOp) Get(orderID int64, riskID int64, options interface{}) (*OrderRisk, error) {
path := fmt.Sprintf("%s/%d/%s/%d.json", ordersRiskBasePath, orderID, ordersRiskResourceName, riskID)
resource := new(OrderRiskResource)
err := s.client.Get(path, resource, options)
return resource.OrderRisk, err
}

// Create order
func (s *OrderRiskServiceOp) Create(orderID int64, orderRisk OrderRisk) (*OrderRisk, error) {
path := fmt.Sprintf("%s/%d/%s.json", ordersRiskBasePath, orderID, ordersRiskResourceName)
wrappedData := OrderRiskResource{OrderRisk: &orderRisk}
resource := new(OrderRiskResource)
err := s.client.Post(path, wrappedData, resource)
return resource.OrderRisk, err
}

// Update order
func (s *OrderRiskServiceOp) Update(orderID int64, riskID int64, orderRisk OrderRisk) (*OrderRisk, error) {
path := fmt.Sprintf("%s/%d/%s/%d.json", ordersRiskBasePath, orderID, ordersRiskResourceName, riskID)
wrappedData := OrderRiskResource{OrderRisk: &orderRisk}
resource := new(OrderRiskResource)
err := s.client.Put(path, wrappedData, resource)
return resource.OrderRisk, err
}

// Delete order
func (s *OrderRiskServiceOp) Delete(orderID int64, riskID int64) error {
path := fmt.Sprintf("%s/%d/%s/%d.json", ordersRiskBasePath, orderID, ordersRiskResourceName, riskID)
err := s.client.Delete(path)
return err
}
Loading

0 comments on commit 6898174

Please sign in to comment.