github.com/plutov/paypal/v4@v4.7.1/integration_test.go (about)

     1  // +build integration
     2  
     3  package paypal
     4  
     5  import (
     6  	"context"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  // All test values are defined here
    14  var testClientID = "AXy9orp-CDaHhBZ9C78QHW2BKZpACgroqo85_NIOa9mIfJ9QnSVKzY-X_rivR_fTUUr6aLjcJsj6sDur"
    15  var testSecret = "EBoIiUSkCKeSk49hHSgTem1qnjzzJgRQHDEHvGpzlLEf_nIoJd91xu8rPOBDCdR_UYNKVxJE-UgS2iCw"
    16  var testUserID = "https://www.paypal.com/webapps/auth/identity/user/VBqgHcgZwb1PBs69ybjjXfIW86_Hr93aBvF_Rgbh2II"
    17  var testCardID = "CARD-54E6956910402550WKGRL6EA"
    18  
    19  var testProductId = ""   // will be fetched in  func TestProduct(t *testing.T)
    20  var testBillingPlan = "" // will be fetched in  func TestSubscriptionPlans(t *testing.T)
    21  
    22  func TestGetAccessToken(t *testing.T) {
    23  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
    24  	token, err := c.GetAccessToken(context.Background())
    25  	if err != nil {
    26  		t.Errorf("Not expected error for GetAccessToken(), got %s", err.Error())
    27  	}
    28  	if token.Token == "" {
    29  		t.Errorf("Expected non-empty token for GetAccessToken()")
    30  	}
    31  }
    32  
    33  func TestGetUserInfo(t *testing.T) {
    34  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
    35  	c.GetAccessToken(context.Background())
    36  
    37  	u, err := c.GetUserInfo(context.Background(), "openid")
    38  	if u.ID != testUserID || err != nil {
    39  		t.Errorf("GetUserInfo must return valid test ID %s, got %s, error: %v", testUserID, u.ID, err)
    40  	}
    41  }
    42  
    43  func TestCreateVenmoPayout(t *testing.T) {
    44  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
    45  	c.GetAccessToken(context.Background())
    46  
    47  	payout := Payout{
    48  		SenderBatchHeader: &SenderBatchHeader{
    49  			SenderBatchID: "Payouts_2018_100007",
    50  			EmailSubject:  "You have a payout!",
    51  			EmailMessage:  "You have received a payout! Thanks for using our service!",
    52  		},
    53  		Items: []PayoutItem{
    54  			{
    55  				RecipientType:   "EMAIL",
    56  				RecipientWallet: VenmoRecipientWallet,
    57  				Receiver:        "receiver@example.com",
    58  				Amount: &AmountPayout{
    59  					Value:    "9.87",
    60  					Currency: "USD",
    61  				},
    62  				Note:         "Thanks for your patronage!",
    63  				SenderItemID: "201403140001",
    64  			},
    65  		},
    66  	}
    67  
    68  	res, err := c.CreatePayout(context.Background(), payout)
    69  	assert.NoError(t, err, "should accept venmo wallet")
    70  	assert.Greater(t, len(res.Items), 0)
    71  }
    72  
    73  func TestCreatePayout(t *testing.T) {
    74  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
    75  	c.GetAccessToken(context.Background())
    76  
    77  	payout := Payout{
    78  		SenderBatchHeader: &SenderBatchHeader{
    79  			SenderBatchID: "Payouts_2018_100007",
    80  			EmailSubject:  "You have a payout!",
    81  			EmailMessage:  "You have received a payout! Thanks for using our service!",
    82  		},
    83  		Items: []PayoutItem{
    84  			{
    85  				RecipientType: "EMAIL",
    86  				Receiver:      "receiver@example.com",
    87  				Amount: &AmountPayout{
    88  					Value:    "9.87",
    89  					Currency: "USD",
    90  				},
    91  				Note:         "Thanks for your patronage!",
    92  				SenderItemID: "201403140001",
    93  			},
    94  		},
    95  	}
    96  
    97  	c.CreatePayout(context.Background(), payout)
    98  }
    99  
   100  func TestStoreCreditCard(t *testing.T) {
   101  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   102  	c.GetAccessToken(context.Background())
   103  
   104  	r1, e1 := c.StoreCreditCard(context.Background(), CreditCard{})
   105  	if e1 == nil || r1 != nil {
   106  		t.Errorf("Error is expected for invalid CC")
   107  	}
   108  
   109  	r2, e2 := c.StoreCreditCard(context.Background(), CreditCard{
   110  		Number:      "4417119669820331",
   111  		Type:        "visa",
   112  		ExpireMonth: "11",
   113  		ExpireYear:  "2020",
   114  		CVV2:        "874",
   115  		FirstName:   "Foo",
   116  		LastName:    "Bar",
   117  	})
   118  	if e2 != nil || r2 == nil {
   119  		t.Errorf("200 code expected for valid CC card. Error: %v", e2)
   120  	}
   121  }
   122  
   123  func TestDeleteCreditCard(t *testing.T) {
   124  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   125  	c.GetAccessToken(context.Background())
   126  
   127  	e1 := c.DeleteCreditCard(context.Background(), "")
   128  	if e1 == nil {
   129  		t.Errorf("Error is expected for invalid CC ID")
   130  	}
   131  }
   132  
   133  func TestGetCreditCard(t *testing.T) {
   134  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   135  	c.GetAccessToken(context.Background())
   136  
   137  	r1, e1 := c.GetCreditCard(context.Background(), "BBGGG")
   138  	if e1 == nil || r1 != nil {
   139  		t.Errorf("Error is expected for invalid CC, got CC %v", r1)
   140  	}
   141  }
   142  
   143  func TestGetCreditCards(t *testing.T) {
   144  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   145  	c.GetAccessToken(context.Background())
   146  
   147  	r1, e1 := c.GetCreditCards(context.Background(), nil)
   148  	if e1 != nil || r1 == nil {
   149  		t.Errorf("200 code expected. Error: %v", e1)
   150  	}
   151  
   152  	r2, e2 := c.GetCreditCards(context.Background(), &CreditCardsFilter{
   153  		Page:     2,
   154  		PageSize: 7,
   155  	})
   156  	if e2 != nil || r2 == nil {
   157  		t.Errorf("200 code expected. Error: %v", e2)
   158  	}
   159  }
   160  
   161  func TestPatchCreditCard(t *testing.T) {
   162  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   163  	c.GetAccessToken(context.Background())
   164  
   165  	r1, e1 := c.PatchCreditCard(context.Background(), testCardID, nil)
   166  	if e1 == nil || r1 != nil {
   167  		t.Errorf("Error is expected for empty update info")
   168  	}
   169  }
   170  
   171  // Creates, gets, and deletes single webhook
   172  func TestCreateAndGetWebhook(t *testing.T) {
   173  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   174  	c.GetAccessToken(context.Background())
   175  
   176  	payload := &CreateWebhookRequest{
   177  		URL: "https://example.com/paypal_webhooks",
   178  		EventTypes: []WebhookEventType{
   179  			WebhookEventType{
   180  				Name: "PAYMENT.AUTHORIZATION.CREATED",
   181  			},
   182  		},
   183  	}
   184  
   185  	createdWebhook, err := c.CreateWebhook(context.Background(), payload)
   186  	if err != nil {
   187  		t.Errorf("Webhook couldn't be created, error %v", err)
   188  	}
   189  
   190  	_, err = c.GetWebhook(context.Background(), createdWebhook.ID)
   191  	if err != nil {
   192  		t.Errorf("An error occurred while getting webhook, error %v", err)
   193  	}
   194  
   195  	err = c.DeleteWebhook(context.Background(), createdWebhook.ID)
   196  	if err != nil {
   197  		t.Errorf("An error occurred while webhooks deletion, error %v", err)
   198  	}
   199  }
   200  
   201  // Creates, updates, and deletes single webhook
   202  func TestCreateAndUpdateWebhook(t *testing.T) {
   203  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   204  	c.GetAccessToken(context.Background())
   205  
   206  	creationPayload := &CreateWebhookRequest{
   207  		URL: "https://example.com/paypal_webhooks",
   208  		EventTypes: []WebhookEventType{
   209  			WebhookEventType{
   210  				Name: "PAYMENT.AUTHORIZATION.CREATED",
   211  			},
   212  		},
   213  	}
   214  
   215  	createdWebhook, err := c.CreateWebhook(context.Background(), creationPayload)
   216  	if err != nil {
   217  		t.Errorf("Webhook couldn't be created, error %v", err)
   218  	}
   219  
   220  	updatePayload := []WebhookField{
   221  		WebhookField{
   222  			Operation: "replace",
   223  			Path:      "/event_types",
   224  			Value: []interface{}{
   225  				map[string]interface{}{
   226  					"name": "PAYMENT.SALE.REFUNDED",
   227  				},
   228  			},
   229  		},
   230  	}
   231  
   232  	_, err = c.UpdateWebhook(context.Background(), createdWebhook.ID, updatePayload)
   233  	if err != nil {
   234  		t.Errorf("Couldn't update webhook, error %v", err)
   235  	}
   236  
   237  	err = c.DeleteWebhook(context.Background(), createdWebhook.ID)
   238  	if err != nil {
   239  		t.Errorf("An error occurred while webhooks deletion, error %v", err)
   240  	}
   241  }
   242  
   243  func TestListWebhooks(t *testing.T) {
   244  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   245  	c.GetAccessToken(context.Background())
   246  
   247  	_, err := c.ListWebhooks(context.Background(), AncorTypeApplication)
   248  	if err != nil {
   249  		t.Errorf("Cannot registered list webhooks, error %v", err)
   250  	}
   251  }
   252  
   253  func TestProduct(t *testing.T) {
   254  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   255  	c.GetAccessToken(context.Background())
   256  
   257  	//create a product
   258  	productData := Product{
   259  		Name:        "Test Product",
   260  		Description: "A Test Product",
   261  		Category:    ProductCategorySoftware,
   262  		Type:        ProductTypeService,
   263  		ImageUrl:    "https://example.com/image.png",
   264  		HomeUrl:     "https://example.com",
   265  	}
   266  
   267  	productCreateResponse, err := c.CreateProduct(context.Background(), productData)
   268  	assert.Equal(t, nil, err)
   269  
   270  	testProductId = productCreateResponse.ID
   271  
   272  	//update the product
   273  	productData.ID = productCreateResponse.ID
   274  	productData.Description = "Updated product"
   275  
   276  	err = c.UpdateProduct(context.Background(), productData)
   277  	assert.Equal(t, nil, err)
   278  
   279  	//get product data
   280  	productFetched, err := c.GetProduct(context.Background(), productData.ID)
   281  	assert.Equal(t, nil, err)
   282  	assert.Equal(t, productFetched.Description, "Updated product")
   283  
   284  	//test that lising products have more than one product
   285  	productList, err := c.ListProducts(context.Background(), nil)
   286  	assert.Equal(t, nil, err)
   287  	assert.NotEqual(t, len(productList.Products), 0)
   288  }
   289  
   290  func TestSubscriptionPlans(t *testing.T) {
   291  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   292  	c.GetAccessToken(context.Background())
   293  
   294  	//create a product
   295  	newSubscriptionPlan := SubscriptionPlan{
   296  		ProductId:   testProductId,
   297  		Name:        "Test subscription plan",
   298  		Status:      SubscriptionPlanStatusCreated,
   299  		Description: "Integration test subscription plan",
   300  		BillingCycles: []BillingCycle{
   301  			{
   302  				PricingScheme: PricingScheme{
   303  					Version: 1,
   304  					FixedPrice: Money{
   305  						Currency: "EUR",
   306  						Value:    "5",
   307  					},
   308  					CreateTime: time.Now(),
   309  					UpdateTime: time.Now(),
   310  				},
   311  				Frequency: Frequency{
   312  					IntervalUnit:  IntervalUnitYear,
   313  					IntervalCount: 1,
   314  				},
   315  				TenureType:  TenureTypeRegular,
   316  				Sequence:    1,
   317  				TotalCycles: 0,
   318  			},
   319  		},
   320  		PaymentPreferences: &PaymentPreferences{
   321  			AutoBillOutstanding:     false,
   322  			SetupFee:                nil,
   323  			SetupFeeFailureAction:   SetupFeeFailureActionCancel,
   324  			PaymentFailureThreshold: 0,
   325  		},
   326  		Taxes: &Taxes{
   327  			Percentage: "19",
   328  			Inclusive:  false,
   329  		},
   330  		QuantitySupported: false,
   331  	}
   332  
   333  	//test create new plan
   334  	planCreateResponse, err := c.CreateSubscriptionPlan(context.Background(), newSubscriptionPlan)
   335  	assert.Equal(t, nil, err)
   336  	testBillingPlan = planCreateResponse.ID // for next test
   337  
   338  	//test update the newly created plan
   339  	newSubscriptionPlan.ID = planCreateResponse.ID
   340  	newSubscriptionPlan.Description = "updated description"
   341  	err = c.UpdateSubscriptionPlan(context.Background(), newSubscriptionPlan)
   342  	assert.Equal(t, nil, err)
   343  
   344  	//test get plan information
   345  	existingPlan, err := c.GetSubscriptionPlan(context.Background(), newSubscriptionPlan.ID)
   346  	assert.Equal(t, nil, err)
   347  	assert.Equal(t, newSubscriptionPlan.Description, existingPlan.Description)
   348  
   349  	//test activate plan
   350  	err = c.ActivateSubscriptionPlan(context.Background(), newSubscriptionPlan.ID)
   351  	assert.Equal(t, nil, err)
   352  
   353  	//test deactivate plan
   354  	err = c.DeactivateSubscriptionPlans(context.Background(), newSubscriptionPlan.ID)
   355  	assert.Equal(t, nil, err)
   356  
   357  	//reactivate this plan for next next (subscription)
   358  	err = c.ActivateSubscriptionPlan(context.Background(), newSubscriptionPlan.ID)
   359  	assert.Equal(t, nil, err)
   360  
   361  	//test upadte plan pricing
   362  	err = c.UpdateSubscriptionPlanPricing(context.Background(), newSubscriptionPlan.ID, []PricingSchemeUpdate{
   363  		{
   364  			BillingCycleSequence: 1,
   365  			PricingScheme: PricingScheme{
   366  				Version: 1,
   367  				FixedPrice: Money{
   368  					Currency: "EUR",
   369  					Value:    "6",
   370  				},
   371  				CreateTime: time.Now(),
   372  				UpdateTime: time.Now(),
   373  			},
   374  		},
   375  	})
   376  	assert.Equal(t, nil, err)
   377  
   378  	//test update pricing scheme
   379  	updatedPricingPlan, err := c.GetSubscriptionPlan(context.Background(), newSubscriptionPlan.ID)
   380  	assert.Equal(t, nil, err)
   381  	assert.Equal(t, "6.0", updatedPricingPlan.BillingCycles[0].PricingScheme.FixedPrice.Value)
   382  
   383  }
   384  
   385  func TestSubscription(t *testing.T) {
   386  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   387  	c.GetAccessToken(context.Background())
   388  
   389  	newSubscription := SubscriptionBase{
   390  		PlanID: testBillingPlan,
   391  	}
   392  
   393  	//create new subscription
   394  	newSubResponse, err := c.CreateSubscription(context.Background(), newSubscription)
   395  	assert.Equal(t, nil, err)
   396  	assert.NotEqual(t, "", newSubResponse.ID)
   397  
   398  	//get subscription details
   399  	subDetails, err := c.GetSubscriptionDetails(context.Background(), newSubResponse.ID)
   400  	assert.Equal(t, nil, err)
   401  	assert.NotEqual(t, "", subDetails.ID)
   402  
   403  }
   404  
   405  func TestGetWebhookEventTypes(t *testing.T) {
   406  	c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
   407  	c.GetAccessToken(context.Background())
   408  
   409  	r, err := c.GetWebhookEventTypes(context.Background())
   410  	assert.Equal(t, nil, err)
   411  	assert.GreaterOrEqual(t, len(r.EventTypes), 1)
   412  	for _, v := range r.EventTypes {
   413  		assert.GreaterOrEqual(t, len(v.Name), 1)
   414  		assert.GreaterOrEqual(t, len(v.Description), 1)
   415  		assert.GreaterOrEqual(t, len(v.Status), 1)
   416  	}
   417  }