<commit_before>package mpower

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/parnurzeal/gorequest"
)

// The onsite definition as defined by mpower documentation
// This struct holds all the data with respect to onsite request
type OnsiteInvoice struct {
	Invoice `json:"invoice_data"`
	OPRData struct {
		Alias string `json:"account_alias"`
	} `json:"opr_data"`
	baseUrl      string `json:"-"`
	ReceiptUrl   string `json:"-"`
	ResponseCode string `json:"-"`
	ResponseText string `json:"-"`
	Description  string `json:"-"`
	Token        string `json:"-"`
	InvoiceToken string `json:"-"`
	Status       string `json:"-"`
	Customer     struct {
		Name  string `json:"-"`
		Phone string `json:"-"`
		Email string `json:"-"`
	} `json:"-"`
}

type responseJsonOnsite struct {
	ResponseCode string `json:"response_code"`
	ResponseText string `json:"response_text"`
	Description  string `json:"description"`
	Token        string `json:"token"`
	InvoiceToken string `json:"invoice_token"`
}

type oprResponse struct {
	ResponseCode string `json:"response_code"`
	ResponseText string `json:"response_text"`
	Description  string `json:"description"`
	InvoiceData  struct {
		ReceiptUrl string `json:"receipt_url"`
		Status     string `json:"status"`
		Customer   struct {
			Name  string `json:"name"`
			Phone string `json:"phone"`
			Email string `json:"email"`
		} `json:"customer"`
	} `json:"invoice_data"`
}

type opr struct {
	token        string `json:"token"`
	confirmToken string `json:"confirm_token"`
}

// Create - creates a bew invoice on mpowers server
// Returns a `boolean` and `error`
// The boolean signifies whether the inovoice was created on not
// The response json object can be retrieved on the onsite invoice object created by the NewOnsiteInvoice
//
// Example.
//
//   ok, err := onsite.Create("hello")
//      if ok {
//         fmt.Printf("%s %s %s %s", onsite.ResponseCode, onsite.ResponseText, onsite.Description, onsite.Token)
//      } else {
//          fmt.Printf("%v", err)
//      }
func (on *OnsiteInvoice) Create(name string) (bool, error) {
	var respJson responseJsonOnsite
	req := gorequest.New()

	on.PrepareForRequest()
	req.Post(on.baseUrl + "/create")

	for key, val := range on.Setup.GetHeaders() {
		req.Set(key, val)
	}

	on.OPRData.Alias = name
	if content, err := json.Marshal(on); err != nil {
		return false, err
	} else {
		req.Send(bytes.NewBuffer(content).String())
	}

	if resp, body, err := req.End(); err != nil {
		on.Status = resp.Status
		return false, fmt.Errorf("%v", err)
	} else {
		if err := json.Unmarshal(bytes.NewBufferString(body).Bytes(), &respJson); err != nil {
			return false, err
		}

		on.ResponseText = respJson.ResponseText
		on.ResponseCode = respJson.ResponseCode
		if respJson.ResponseCode == "00" {
			on.Description = respJson.Description
			on.Token = respJson.Token
			on.InvoiceToken = respJson.InvoiceToken

			return true, nil
		}

		return false, fmt.Errorf("Failed to create invoice with error : %s", body)
	}
}

// Charge - it charges the customer on mpower and returns a response json object which contains the receipt url with other information
// The `confirmToken` is from the customer
// Returns a `boolean` and `error`
// The boolean signifies whether the customer was chargeed or not
// The response json object can be retrieved on the onsite invoice object
//
// Example.
//    if ok, err := onsite.Charge(onsite.Token, "4346"); ok {
//      //doSomething
//    } else {
//
//    }
//
func (on *OnsiteInvoice) Charge(oprToken, confirmToken string) (bool, error) {
	var respData oprResponse
	data := opr{oprToken, confirmToken}
	req := gorequest.New()

	for key, val := range on.Setup.GetHeaders() {
		req.Set(key, val)
	}

	if dataByte, err := json.Marshal(data); err != nil {
		return false, err
	} else {<commit_msg>post request to `charge` url<commit_after>package mpower

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/parnurzeal/gorequest"
)

// The onsite definition as defined by mpower documentation
// This struct holds all the data with respect to onsite request
type OnsiteInvoice struct {
	Invoice `json:"invoice_data"`
	OPRData struct {
		Alias string `json:"account_alias"`
	} `json:"opr_data"`
	baseUrl      string `json:"-"`
	ReceiptUrl   string `json:"-"`
	ResponseCode string `json:"-"`
	ResponseText string `json:"-"`
	Description  string `json:"-"`
	Token        string `json:"-"`
	InvoiceToken string `json:"-"`
	Status       string `json:"-"`
	Customer     struct {
		Name  string `json:"-"`
		Phone string `json:"-"`
		Email string `json:"-"`
	} `json:"-"`
}

type responseJsonOnsite struct {
	ResponseCode string `json:"response_code"`
	ResponseText string `json:"response_text"`
	Description  string `json:"description"`
	Token        string `json:"token"`
	InvoiceToken string `json:"invoice_token"`
}

type oprResponse struct {
	ResponseCode string `json:"response_code"`
	ResponseText string `json:"response_text"`
	Description  string `json:"description"`
	InvoiceData  struct {
		ReceiptUrl string `json:"receipt_url"`
		Status     string `json:"status"`
		Customer   struct {
			Name  string `json:"name"`
			Phone string `json:"phone"`
			Email string `json:"email"`
		} `json:"customer"`
	} `json:"invoice_data"`
}

type opr struct {
	token        string `json:"token"`
	confirmToken string `json:"confirm_token"`
}

// Create - creates a bew invoice on mpowers server
// Returns a `boolean` and `error`
// The boolean signifies whether the inovoice was created on not
// The response json object can be retrieved on the onsite invoice object created by the NewOnsiteInvoice
//
// Example.
//
//   ok, err := onsite.Create("hello")
//      if ok {
//         fmt.Printf("%s %s %s %s", onsite.ResponseCode, onsite.ResponseText, onsite.Description, onsite.Token)
//      } else {
//          fmt.Printf("%v", err)
//      }
func (on *OnsiteInvoice) Create(name string) (bool, error) {
	var respJson responseJsonOnsite
	req := gorequest.New()

	on.PrepareForRequest()
	req.Post(on.baseUrl + "/create")

	for key, val := range on.Setup.GetHeaders() {
		req.Set(key, val)
	}

	on.OPRData.Alias = name
	if content, err := json.Marshal(on); err != nil {
		return false, err
	} else {
		req.Send(bytes.NewBuffer(content).String())
	}

	if resp, body, err := req.End(); err != nil {
		on.Status = resp.Status
		return false, fmt.Errorf("%v", err)
	} else {
		if err := json.Unmarshal(bytes.NewBufferString(body).Bytes(), &respJson); err != nil {
			return false, err
		}

		on.ResponseText = respJson.ResponseText
		on.ResponseCode = respJson.ResponseCode
		if respJson.ResponseCode == "00" {
			on.Description = respJson.Description
			on.Token = respJson.Token
			on.InvoiceToken = respJson.InvoiceToken

			return true, nil
		}

		return false, fmt.Errorf("Failed to create invoice with error : %s", body)
	}
}

// Charge - it charges the customer on mpower and returns a response json object which contains the receipt url with other information
// The `confirmToken` is from the customer
// Returns a `boolean` and `error`
// The boolean signifies whether the customer was chargeed or not
// The response json object can be retrieved on the onsite invoice object
//
// Example.
//    if ok, err := onsite.Charge(onsite.Token, "4346"); ok {
//      //doSomething
//    } else {
//
//    }
//
func (on *OnsiteInvoice) Charge(oprToken, confirmToken string) (bool, error) {
	var respData oprResponse
	data := opr{oprToken, confirmToken}
	req := gorequest.New()

	req.Post(on.baseUrl + "/charge")
	for key, val := range on.Setup.GetHeaders() {
		req.Set(key, val)
	}

	if dataByte, err := json.Marshal(data); err != nil {
		return false, err
	} else {
{'fraction_non_alphanumeric': 0.13539903658377817, 'fraction_numerical': 0.001562296575966671, 'mean_word_length': 3.611044417767107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64210', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2456, 'n_words': 744}