This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
/
ExampleUsage.php
258 lines (212 loc) · 7.79 KB
/
ExampleUsage.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
$path_to_BillForward = 'lib/BillForward.php';
require_once($path_to_BillForward);
//namespace BFPHPClientTest;
//echo "Running Bf_Account tests for BillForward PHP Client Library.\n";
//use BFPHPClient\Account;
function getUsualAccountsProfileEmail() {
return 'full@account.is.moe';
}
function getUsualPrpName() {
return 'Cool Plan';
}
// Grab an API token from: https://app-sandbox.billforward.net/setup/#/personal/api-keys
$access_token = 'INSERT ACCESS TOKEN HERE';
$urlRoot = 'https://api-sandbox.billforward.net/2014.223.0/';
$client = new BillForwardClient($access_token, $urlRoot);
BillForwardClient::setDefaultClient($client);
//-- Find the account we login with (assume first found with associated user)
// order by userID so that we are likely to see our login user's account
$accounts = Bf_Account::getAll(array(
'order_by' => 'userID'
));
$foundLoginAccount = NULL;
foreach ($accounts as $account) {
if (array_key_exists('userID', $account)) {
$foundLoginAccount = $account;
break;
}
}
if (is_null($foundLoginAccount)) {
throw new Exception('Login account not found.');
}
//var_export($foundLoginAccount);
//-- Get the organization we log in with (assume first found)
$orgs = Bf_Organisation::getMine();
$firstOrg = $orgs[0];
$firstOrgID = $firstOrg->id;
// we are going to add an API configuration for Authorize.Net
$configType = "AuthorizeNetConfiguration";
// Create (upon our organisation) API configuration for Authorize.net
$AuthorizeNetLoginID = 'FILL IN WITH AUTHORIZE NET LOGIN ID';
$AuthorizeNetTransactionKey = 'FILL IN WITH AUTHORIZE NET TRANSACTION KEY';
// model of Authorize.Net credentials
$apiConfiguration = new Bf_ApiConfiguration(array(
"@type" => $configType,
"APILoginID" => $AuthorizeNetLoginID,
"transactionKey" => $AuthorizeNetTransactionKey,
"environment" => "Sandbox"
));
// when there are no api configurations, possibly there is no array altogether
if (!is_array($firstOrg->apiConfigurations)) {
$firstOrg->apiConfigurations = array();
}
// we are going to remove any existing API configurations of the current type
$prunedConfigs = array();
foreach($firstOrg->apiConfigurations as $config) {
if ($config['@type'] !== $configType) {
array_push($prunedConfigs, $config);
}
}
// add to our organization the model of the Authorize.Net credentials
array_push($prunedConfigs, $apiConfiguration);
$firstOrg
->apiConfigurations = $prunedConfigs;
$savedOrg = $firstOrg
->save();
//-- Make account with expected profile
$email = getUsualAccountsProfileEmail();
$profile = new Bf_Profile(array(
'email' => $email,
'firstName' => 'Test',
));
$account = new Bf_Account(array(
'profile' => $profile,
));
$createdAcc = Bf_Account::create($account);
$createdAccID = $createdAcc->id;
//-- make payment method, and associate it with account
//-- make Authorize.net token to associate payment method to
// FILL IN WITH YOUR AUTHORIZE.NET CUSTOMER PROFILE ID
$customerProfileID = 00000000;
// FILL IN WITH YOUR AUTHORIZE.NET CUSTOMER PAYMENT PROFILE ID
$customerPaymentProfileID = 00000000;
// FILL IN WITH YOUR AUTHORIZE.NET CUSTOMER'S CARD LAST 4 DIGITS
// this 'last 4 digits of credit card number' field (currently optional) is required for refunds
$cardLast4Digits = 0000;
$authorizeNetToken = new Bf_AuthorizeNetToken(array(
'accountID' => $createdAccID,
'customerProfileID' => $customerProfileID,
'customerPaymentProfileID' => $customerPaymentProfileID,
'lastFourDigits' => $cardLast4Digits,
));
$createdAuthorizeNetToken = Bf_AuthorizeNetToken::create($authorizeNetToken);
$createdAuthorizeNetTokenID = $createdAuthorizeNetToken
->id;
$paymentMethod = new Bf_PaymentMethod(array(
'linkID' => $createdAuthorizeNetTokenID,
'accountID' => $createdAccID,
'name' => 'Authorize.Net',
'description' => 'Pay via Authorize.Net',
'gateway' => 'authorizeNet',
'userEditable' => 0,
'priority' => 100,
'reusable' => 1,
));
$createdPaymentMethod = Bf_PaymentMethod::create($paymentMethod);
$createdPaymentMethodID = $createdPaymentMethod->id;
$paymentMethods = array($createdPaymentMethod);
// add these payment methods to our model of the created account
$createdAcc
->paymentMethods = $paymentMethods;
// save changes to real account
$createdAcc = $createdAcc
->save();
var_export($createdAcc);
//-- Make unit of measure
$uom = new Bf_UnitOfMeasure(array(
'name' => 'Devices',
'displayedAs' => 'Devices',
'roundingScheme' => 'UP',
));
$createdUom = Bf_UnitOfMeasure::create($uom);
$createdUomID = $createdUom->id;
//-- Make product
$product = new Bf_Product(array(
'productType' => 'non-recurring',
'state' => 'prod',
'name' => 'Month of Paracetamoxyfrusebendroneomycin',
'description' => 'It can cure the common cold, and being struck by lightning',
'durationPeriod' => 'days',
'duration' => 28,
));
$createdProduct = Bf_Product::create($product);
$createdProductID = $createdProduct->id;
//-- Make product rate plan
//-- Make pricing components for product rate plan
//-- Make tiers for pricing component
$tier = new Bf_PricingComponentTier(array(
'lowerThreshold' => 1,
'upperThreshold' => 1,
'pricingType' => 'unit',
'price' => 1,
));
$tiers = array($tier);
$pricingComponentsArray = array(
new Bf_PricingComponent(array(
'@type' => 'flatPricingComponent',
'chargeModel' => 'flat',
'name' => 'Devices used',
'description' => 'How many devices you use, I guess',
'unitOfMeasureID' => $createdUomID,
'chargeType' => 'subscription',
'upgradeMode' => 'immediate',
'downgradeMode' => 'immediate',
'defaultQuantity' => 10,
'tiers' => $tiers
))
);
$prp = new Bf_ProductRatePlan(array(
'currency' => 'USD',
'name' => getUsualPrpName(),
'pricingComponents' => $pricingComponentsArray,
'productID' => $createdProductID,
));
$createdPrp = Bf_ProductRatePlan::create($prp);
$createdProductRatePlanID = $createdPrp->id;
$createdPricingComponentID = $createdPrp->pricingComponents[0]->id;
//-- Make pricing component value instance of pricing component
$prc = new Bf_PricingComponentValue(array(
'pricingComponentID' => $createdPricingComponentID,
'value' => 2,
'crmID' => ''
));
$pricingComponentValuesArray = array($prc);
//-- Make Bf_PaymentMethodSubscriptionLinks
// refer by ID to our payment method.
$paymentMethodReference = new Bf_PaymentMethod(array(
'id' => $createdPaymentMethodID
));
$paymentMethodSubscriptionLink = new Bf_PaymentMethodSubscriptionLink(array(
// 'paymentMethodID' => $createdPaymentMethodID,
'paymentMethod' => $paymentMethodReference,
'organizationID' => $firstOrgID,
));
$paymentMethodSubscriptionLinks = array($paymentMethodSubscriptionLink);
//-- Provision subscription
$sub = new Bf_Subscription(array(
'type' => 'Subscription',
'productID' => $createdProductID,
'productRatePlanID' => $createdProductRatePlanID,
'accountID' => $createdAccID,
'name' => 'Memorable Bf_Subscription',
'description' => 'Memorable Bf_Subscription Description',
'paymentMethodSubscriptionLinks' => $paymentMethodSubscriptionLinks,
'pricingComponentValues' => $pricingComponentValuesArray
));
$createdSub = Bf_Subscription::create($sub);
// activate provisioned subscription
$createdSub
->activate();
echo "\n";
echo sprintf("\$usualLoginAccountID = '%s';\n", $foundLoginAccount->id);
echo sprintf("\$usualLoginUserID = '%s';\n", $foundLoginAccount->userID);
echo sprintf("\$usualOrganisationID = '%s';\n", $firstOrgID);
echo sprintf("\$usualAccountID = '%s';\n", $createdAccID);
echo sprintf("\$usualPaymentMethodLinkID = '%s';\n", $createdAuthorizeNetTokenID);
echo sprintf("\$usualPaymentMethodID = '%s';\n", $createdPaymentMethodID);
echo sprintf("\$usualProductID = '%s';\n", $createdProductID);
echo sprintf("\$usualProductRatePlanID = '%s';\n", $createdProductRatePlanID);
echo sprintf("\$usualPricingComponentID = '%s';\n", $createdPricingComponentID);
echo sprintf("\$usualSubscriptionID = '%s';\n", $createdSub->id);
echo sprintf("\$usualUnitOfMeasureID = '%s';\n", $createdUomID);