Ejemplo n.º 1
0
// On non-conflict errors, retry cancelling the order later
func (order *Order) doCancel(success bool, log string) error {
	cancelLog := log

	// Try to cancel the shipment, if necessary
	shipment, err := catalogApi.GetShipment(order.shop.catalogEndpoint, order.ShipmentId)
	if err != nil {
		return err
	}
	if !success && shipment.Status != catalogApi.ShipmentCancelled {
		err := catalogApi.CancelShipment(order.shop.catalogEndpoint, order.ShipmentId)
		if err != nil {
			if isConflictError(err) {
				cancelLog += fmt.Sprintf("\nError cancelling shipment: %v", err)
			} else {
				return err
			}
		} else {
			cancelLog += "\nShipment cancelled"
		}
	} else {
		cancelLog += "\nShipment " + string(shipment.Status)
	}

	// Try to cancel the payment, if necessary
	payment, err := paymentApi.FetchPayment(order.shop.paymentEndpoint, order.PaymentId)
	if err != nil {
		return err
	}
	if !success && payment.Status != paymentApi.PaymentFailed {
		err := paymentApi.CancelPayment(order.shop.paymentEndpoint, order.PaymentId)
		if err != nil {
			if isConflictError(err) {
				cancelLog += fmt.Sprintf("\nError cancelling payment: %v", err)
			} else {
				return err
			}
		} else {
			cancelLog += "\nPayment cancelled"
		}
	}
	cancelLog += "\nPayment was " + payment.Status
	if payment.Error != "" {
		cancelLog += ", error: " + payment.Error
	}

	// Remove the order from the list of open orders & store cancel log
	return order.shop.redis.Transaction(func(redis services.Redis) error {
		order.Status = cancelLog
		err := redis.Cmd("srem", open_orders_key, order.id).Err()
		if err != nil {
			return err
		}
		return order.SaveIn(redis)
	})
}
Ejemplo n.º 2
0
func (order *Order) commitPayment() bool {
	payment, err := paymentApi.FetchPayment(order.shop.paymentEndpoint, order.PaymentId)
	if order.checkError(err) {
		return false
	}
	switch payment.Status {
	case paymentApi.PaymentCreated, paymentApi.PaymentPending:
		err := paymentApi.CommitPayment(order.shop.paymentEndpoint, order.PaymentId)
		_ = order.checkError(err)
		// TODO maybe check that state changed to committed?
		return false
	case paymentApi.PaymentCommitted:
		// Waiting for payment to be processed
		return false
	case paymentApi.PaymentProcessed:
		return true
	default:
		order.Cancel(fmt.Errorf("Payment has status %v", payment.Status))
		return false
	}
}