summaryrefslogtreecommitdiff
path: root/ord.c
blob: 76c54ce9c8ee07f998144bc578f4db40e88acb53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "request.h"
#include "interchange.h"
#include "interchange/member.h"
#include "interchange/ord.h"

void interchange_ord_order(const char *sku, const char *item,
		const unsigned int quantity, const char *options[][2],
		void (*parser)(interchange_response *))
{
	size_t length = 0;
	unsigned int qty = quantity;
	do {
		length++;
	} while ((qty /= 10));
	char *qty_str = malloc(length + 1);
	sprintf(qty_str, "%d", quantity);
	const char **pair = *options;
	size_t nopts = 0;
	while (pair[0] && pair[1]) {
		nopts++;
		pair = *++options;
	}
	for (size_t i = 0; i < nopts; i++)
		--options;
	size_t total_nopts = 4 + (nopts ? 1 : 0) + nopts * 2;
	const char *order[total_nopts + 1][2];
	order[0][0] = "mv_action";
	order[0][1] = "refresh";
	order[1][0] = "mv_sku";
	order[1][1] = sku;
	order[2][0] = "mv_order_item";
	order[2][1] = item;
	order[3][0] = "mv_order_quantity";
	order[3][1] = qty_str;
	static const char *prefix = "mv_order_";
	if (nopts) {
		order[4][0] = "mv_form_profile";
		order[4][1] = "check_opt";
	}
	for (size_t i = 0; i < nopts; i++) {
		const char **pair = options[i];
		size_t j = i * 2;
		order[5 + j][0] = malloc(strlen(prefix) + strlen(pair[0]) + 1);
		sprintf((char *)order[5 + j][0], "%s%s", prefix, pair[0]);
		order[5 + j][1] = malloc(strlen(pair[1]) + 1);
		strcpy((char *)order[5 + j][1], pair[1]);
		order[6 + j][0] = "mv_item_option";
		order[6 + j][1] = malloc(strlen(pair[0]) + 1);
		strcpy((char *)order[6 + j][1], pair[0]);
	}
	order[total_nopts][0] = NULL;
	request(parser, NULL, order, "%s", "ord/basket");
}

void interchange_ord_update(const char *name, const unsigned int quantity,
		const char *orderpage, const char *nextpage,
		void (*parser)(interchange_response *))
{
	size_t length = 0;
	unsigned int qty = quantity;
	do {
		length++;
	} while ((qty /= 10));
	char *qty_str = malloc(length + 1);
	sprintf(qty_str, "%d", quantity);
	request(parser, NULL, (const char *[][2]){
		"mv_quantity_update", "1",
		"mv_doit", "refresh",
		name, qty_str,
		"mv_orderpage", orderpage ? orderpage : "ord/basket",
		"mv_nextpage", nextpage,
		NULL
	}, "%s", "process");
}

void interchange_ord_checkout(const char *order_profile,
		struct interchange_member member,
		void (*handler)(interchange_response *))
{
	request(handler, NULL, (const char *[][2]){
		"mv_todo", "submit",
		"mv_action", "refresh",
		"mv_order_profile", order_profile,
		"fname", member.fname,
		"lname", member.lname,
		"address1", member.address1,
		"address2", member.address2,
		"city", member.city,
		"state", member.state,
		"zip", member.zip,
		"email", member.email,
		"phone_day", member.phone_day,
		"mv_same_billing",
		member.preferences.mv_same_billing ? "1" : "0",
		"email_copy", member.preferences.email_copy? "1" : "0",
		NULL
	}, "%s", "ord/checkout");
}

void interchange_ord_free_order(struct interchange_ord_order *order)
{
	if (order->profile)
		free(order->profile);
	free(order);
}

void interchange_ord_clear_transaction(struct interchange_ord_transaction
		*transaction)
{
	if (transaction->order_number)
		free(transaction->order_number);
	if (transaction->payment_method)
		free(transaction->payment_method);
}