| 1 |  |  | package ynote | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	"context" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	"net/http" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	"testing" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	"github.com/NdoleStudio/ynote-go/internal/helpers" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	"github.com/NdoleStudio/ynote-go/internal/stubs" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	"github.com/stretchr/testify/assert" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | ) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | func TestRefundService_Refund(t *testing.T) { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | 	// Setup | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	t.Parallel() | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	// Arrange | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 	requests := make([]http.Request, 0) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 	responses := [][]byte{stubs.TokenResponse(), stubs.RefundResponse()} | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests) | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 	client := New( | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 		WithTokenURL(server.URL), | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 		WithAPIURL(server.URL), | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		WithClientID(testClientID), | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		WithClientSecret(testClientSecret), | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 	) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	payload := &RefundParams{ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		ChannelUserMsisdn:         "699999999", | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 		Pin:                       "0000", | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		Webhook:                   "https://example.com/webhook", | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 		Amount:                    "100", | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 		FinalCustomerPhone:        "699999999", | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 		FinalCustomerName:         "", | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 		RefundMethod:              "OrangeMoney", | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 		FeesIncluded:              false, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		FinalCustomerNameAccuracy: "0", | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	// Act | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	transaction, response, err := client.Refund.Refund(context.Background(), payload) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	// Assert | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	assert.Nil(t, err) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	assert.GreaterOrEqual(t, len(requests), 2) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	request := requests[len(requests)-1] | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	assert.Equal(t, "/prod/refund", request.URL.Path) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	assert.Equal(t, "Bearer 19077204-9d0a-31fa-85cf-xxxxxxxxxx", request.Header.Get("Authorization")) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	assert.Equal(t, "993764f9-6b1f-41bd-a7ca-97b8b2167ed7", transaction.MessageID) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 	// Teardown | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	server.Close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | func TestRefundService_RefundWithInvalidClient(t *testing.T) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	// Setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	t.Parallel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	// Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	requests := make([]http.Request, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	responses := [][]byte{stubs.TokenResponse(), stubs.RefundInvalidClientResponse()} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusBadRequest}, responses, &requests) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	client := New( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		WithTokenURL(server.URL), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		WithAPIURL(server.URL), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		WithClientID(testClientID), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		WithClientSecret(testClientSecret), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	payload := &RefundParams{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		ChannelUserMsisdn:         "699999999", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		Pin:                       "0000", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		Webhook:                   "https://api.nyangapay.com/v1/y-note", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		Amount:                    "100", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		FinalCustomerPhone:        "699999999", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		FinalCustomerName:         "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		RefundMethod:              "OrangeMoney", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		FeesIncluded:              false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		FinalCustomerNameAccuracy: "0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	// Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	transaction, response, err := client.Refund.Refund(context.Background(), payload) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	// Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	assert.Nil(t, transaction) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	assert.NotNil(t, err) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	assert.Equal(t, http.StatusBadRequest, response.HTTPResponse.StatusCode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	// Teardown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	server.Close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | func TestRefundService_Status(t *testing.T) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	// Setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	t.Parallel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	// Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	requests := make([]http.Request, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	responses := [][]byte{stubs.TokenResponse(), stubs.RefundStatusResponse()} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	client := New( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		WithTokenURL(server.URL), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		WithAPIURL(server.URL), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		WithClientID(testClientID), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		WithClientSecret(testClientSecret), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	// Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	transaction, response, err := client.Refund.Status(context.Background(), "") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	// Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	assert.Nil(t, err) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	assert.Equal(t, "CI24120168FBF65A909F588B4480", transaction.Result.Data.PayToken) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	// Teardown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	server.Close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | func TestRefundService_StatusWithError(t *testing.T) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	// Setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	t.Parallel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	// Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	requests := make([]http.Request, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	responses := [][]byte{stubs.TokenResponse(), []byte("Transactions not found")} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	client := New( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		WithTokenURL(server.URL), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		WithAPIURL(server.URL), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		WithClientID(testClientID), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		WithClientSecret(testClientSecret), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	// Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	_, response, err := client.Refund.Status(context.Background(), "ddd") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	// Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	assert.NotNil(t, err) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	// Teardown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	server.Close() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |