Skip to content

Commit

Permalink
fix(itemsform): provide default subscription parameters
Browse files Browse the repository at this point in the history
provides default subscription parameters and allows user to change to valid values.

fix #11
  • Loading branch information
ndvo committed Sep 25, 2020
1 parent 7be3801 commit d2fd62c
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 14 deletions.
100 changes: 99 additions & 1 deletion src/elements/public/ItemsForm/ItemsForm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,14 @@ describe('The form should add frequency fields', async function () {

describe('The form submits a valid POST to forxycart', async function () {
const sig64 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
const signatures = { name: sig64, code: sig64, price: sig64, quantity: sig64 };
const signatures = {
name: sig64,
code: sig64,
price: sig64,
quantity: sig64,
sub_modify: sig64,
sub_restart: sig64,
};
let xhr: sinon.SinonFakeXMLHttpRequestStatic;
let requests: sinon.SinonFakeXMLHttpRequest[];
let logSpy: sinon.SinonStub;
Expand Down Expand Up @@ -578,6 +585,97 @@ describe('The form submits a valid POST to forxycart', async function () {
}
expect(freqStartEnd).to.deep.equal([3, 3, 3]);
});

it('Defaults sub_modify to replace', async function () {
const el = await formWith2items(10, 10);
await elementUpdated(el);
const form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_modify/)) {
expect(e[1]).to.equal('replace');
}
}
});

it('Allow user to set sub_modify to append mode', async function () {
let el = await fixture(html`
<test-items-form currency="usd" store="test.foxycart.com" sub_modify="">
<x-testitem name="p1" code="MyCode" price="10.00" quantity="3"></x-testitem>
</test-items-form>
`);
await elementUpdated(el);
let form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_modify/)) {
expect(e[1]).to.equal('');
}
}
el = await fixture(html`
<test-items-form currency="usd" store="test.foxycart.com" sub_modify="append">
<x-testitem name="p1" code="MyCode" price="10.00" quantity="3"></x-testitem>
</test-items-form>
`);
await elementUpdated(el);
form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_modify/)) {
expect(e[1]).to.equal('');
}
}
});

it('Defults sub_restart to auto', async function () {
const el = await formWith2items(10, 10);
await elementUpdated(el);
const form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_restart/)) {
expect(e[1]).to.equal('auto');
}
}
});

it('Allows user to set sub_restart to true', async function () {
let el = await fixture(html`
<test-items-form currency="usd" store="test.foxycart.com" sub_restart="true">
<x-testitem name="p1" code="MyCode" price="10.00" quantity="3"></x-testitem>
</test-items-form>
`);
await elementUpdated(el);
let form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_restart/)) {
expect(e[1]).to.equal('true');
}
}
el = await fixture(html`
<test-items-form currency="usd" store="test.foxycart.com" sub_restart="anythingelse">
<x-testitem name="p1" code="MyCode" price="10.00" quantity="3"></x-testitem>
</test-items-form>
`);
await elementUpdated(el);
form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_restart/)) {
expect(e[1]).to.equal('auto');
}
}
});

it('Allow user to set sub_token', async function () {
const el = await fixture(html`
<test-items-form currency="usd" store="test.foxycart.com" sub_token="retrievedurl">
<x-testitem name="p1" code="MyCode" price="10.00" quantity="3"></x-testitem>
</test-items-form>
`);
await elementUpdated(el);
const form = el.shadowRoot!.querySelector('form') as HTMLFormElement;
for (const e of new FormData(form).entries()) {
if (e[0].match(/.*sub_token/)) {
expect(e[1]).to.equal('retrievedurl');
}
}
});
});
describe('The form directs the user to the propper destination', async function () {
it('Uses the _top window', async function () {
Expand Down
88 changes: 75 additions & 13 deletions src/elements/public/ItemsForm/ItemsForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ export class ItemsForm extends Translatable {
return value;
},
},
sub_token: { type: String },
sub_modify: {
type: String,
converter: value => {
if (value === '' || value === 'append') {
return '';
} else {
return 'replace';
}
},
},
sub_restart: {
type: String,
converter: value => {
if (value === 'true') {
return value;
} else {
return 'auto';
}
},
},
frequencies: {
converter: (value, type) => {
if (!value) {
Expand Down Expand Up @@ -157,20 +178,51 @@ export class ItemsForm extends Translatable {
* it is assumed to be next occurence of that day of the month, from the
* current date.
*
* For more options https://wiki.foxycart.com/v/2.0/products#a_complete_list_of_product_parameters
* See [Products wiki for more details](https://wiki.foxycart.com/v/2.0/products#a_complete_list_of_product_parameters)
*
* ** Example:** `"10"`
*/
public sub_startdate?: string;

/**
* Optional subscription token, unique URL of a subscription, retrieved from
* the API, XML datafeeds or Admin interface.
*/
public sub_token?: string;

/**
* Optional. Allows the “add to cart” link or form to completely replace the
* existing subscription loaded
*
* Can be either "replace", "append" or "" (a blank string).
* Using "append" or "" will result in adding these items to an existing
* subscription in addition to the existing ones.
*
* The "append" value is set for convenience, as it describes the behaviour.
* The submitted value, in this case, will be "". The submitted value, in
* this case, will be "".
*
* Using "replace" results in replacing the existing subscription with the
* current itmes.
* See [Products subscription options](https://wiki.foxycart.com/v/2.0/products/subscriptions#subscription-related_product_options)
*/
public sub_modify = 'replace';

/**
* Set to "true" to indicate that payment is collectable right now.
* Set to "auto" to indicate that payment is collectable right now if the
* subscription's past-due amount is greater than 0
*/
public sub_restart = 'auto';

/**
* Optional subscription end date encoded as four integer for the year, two
* for the month and two for the day.
*
* The absence of a sub_enddate, together with a sub_frequency, means a
* subscription with indefinite and date.
*
* For more options https://wiki.foxycart.com/v/2.0/products#a_complete_list_of_product_parameters
* See [Products wiki for more details](https://wiki.foxycart.com/v/2.0/products#a_complete_list_of_product_parameters)
*
* ** Example:** `"20221010"`
*/
Expand Down Expand Up @@ -264,8 +316,8 @@ export class ItemsForm extends Translatable {
* - items: an array of other items that are to be treated as bundled with this item
* - signatures: an object containing a key value list of previously generated HMAC validation codes
*
* Other item properties are accepted and sent to foxy cart.
* https://wiki.foxycart.com/v/2.0/products#a_complete_list_of_product_parameters
* Other item properties are accepted and sent to foxy cart
* See [Products wiki for more details](https://wiki.foxycart.com/v/2.0/products#a_complete_list_of_product_parameters)
*/
public set items(value: ItemInterface[]) {
this.__removeItems();
Expand Down Expand Up @@ -462,7 +514,7 @@ export class ItemsForm extends Translatable {
}
}

// builtd a key with prepended id and appended signature and |open given an item
// build a key with prepended id and appended signature and |open given an item
private __buildKeyFromItem(key: string, item: ItemInterface) {
return this.__buildKey(
item.pid!.toString(),
Expand All @@ -486,18 +538,27 @@ export class ItemsForm extends Translatable {
* @argument {FormData} fd the FormData to which subscription fields will be added
**/
private __formDataAddSubscriptionFields(fd: FormData, item: ItemInterface): void {
// added if sub_frequency is set
if (this.sub_frequency) {
const subKey = this.__buildKeyFromItem('sub_frequency', item);
fd.set(subKey, this.sub_frequency!);
if (this.sub_startdate) {
const subStart = this.__buildKeyFromItem('sub_startdate', item);
fd.set(subStart, this.sub_startdate!);
for (const s of ['sub_frequency', 'sub_startdate', 'sub_enddate']) {
if ((this as any)[s]) {
const subKey = this.__buildKeyFromItem(s, item);
fd.set(subKey, (this as any)[s]);
}
}
if (this.sub_enddate) {
const subEnd = this.__buildKeyFromItem('sub_enddate', item);
fd.set(subEnd, this.sub_enddate!);
}
// added if themselves are set
for (const s of ['sub_token']) {
if ((this as any)[s]) {
const subKey = this.__buildKeyFromItem(s, item);
fd.set(subKey, (this as any)[s]);
}
}
// added regardless
for (const s of ['sub_modify', 'sub_restart']) {
const subKey = this.__buildKeyFromItem(s, item);
fd.set(subKey, (this as any)[s]);
}
}

/**
Expand All @@ -517,6 +578,7 @@ export class ItemsForm extends Translatable {
* @argument string strDate the date as a string to be used as start or end date.
*
* https://wiki.foxycart.com/v/2.0/products#subscription_product_options
* See [Products subscription options for more details](https://wiki.foxycart.com/v/2.0/products#subscription_product_options)
*/
private static __validDate(strDate: string | null | undefined): boolean {
if (strDate === null || strDate === undefined) {
Expand Down

0 comments on commit d2fd62c

Please sign in to comment.