-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14b6bd0
commit f4c2bde
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package zibal | ||
|
||
import ( | ||
"encoding/json" | ||
e "github.com/mohammadv184/gopayment/errors" | ||
"strconv" | ||
|
||
"github.com/mohammadv184/gopayment/helpers" | ||
|
||
"github.com/mohammadv184/gopayment/invoice" | ||
) | ||
|
||
// Purchase sends a request to zibal to purchase an invoice. | ||
func (d *Driver) Purchase(invoice *invoice.Invoice) (string, error) { | ||
var reqBody = map[string]interface{}{ | ||
"merchant": d.Merchant, | ||
"callbackUrl": d.Callback, | ||
"description": invoice.GetDescription(), | ||
"orderId": invoice.GetUUID(), | ||
"amount": invoice.GetAmount(), | ||
} | ||
if d := invoice.GetDetail("phone"); d != "" { | ||
reqBody["mobile"] = d | ||
} | ||
resp, _ := client.Post(APIPurchaseURL, reqBody, nil) | ||
var res map[string]interface{} | ||
err := json.Unmarshal(resp.Body(), &res) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if resp.StatusCode() != 200 || res["result"].(float64) != 100 { | ||
return "", e.ErrPurchaseFailed{ | ||
Message: res["message"].(string), | ||
} | ||
} | ||
|
||
return strconv.Itoa(int(res["trackId"].(float64))), nil | ||
} | ||
|
||
// PayURL returns the url to redirect the user to in order to pay the invoice. | ||
func (d *Driver) PayURL(invoice *invoice.Invoice) string { | ||
return APIPaymentURL + invoice.GetTransactionID() | ||
} | ||
|
||
// PayMethod returns the Request Method to be used to pay the invoice. | ||
func (d *Driver) PayMethod() string { | ||
return "GET" | ||
} | ||
|
||
// RenderRedirectForm renders the html form for redirect to payment page. | ||
func (d *Driver) RenderRedirectForm(invoice *invoice.Invoice) (string, error) { | ||
return helpers.RenderRedirectTemplate(d.PayMethod(), d.PayURL(invoice), nil) | ||
} |