|
1 | 1 | <?php |
2 | 2 | require 'vendor/autoload.php'; |
| 3 | + |
3 | 4 | use net\authorize\api\contract\v1 as AnetAPI; |
4 | 5 | use net\authorize\api\controller as AnetController; |
5 | 6 |
|
6 | 7 | define("AUTHORIZENET_LOG_FILE", "phplog"); |
7 | 8 |
|
8 | | - function createCustomerProfileWithAcceptNonce($email){ |
9 | | - |
10 | | - // Common setup for API credentials |
11 | | - $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); |
12 | | - $merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID); |
13 | | - $merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY); |
14 | | - $refId = 'ref' . time(); |
| 9 | +function createCustomerProfileWithAcceptNonce($email) |
| 10 | +{ |
| 11 | + /* Create a merchantAuthenticationType object with authentication details |
| 12 | + retrieved from the constants file */ |
| 13 | + $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); |
| 14 | + $merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID); |
| 15 | + $merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY); |
| 16 | + |
| 17 | + // Set the transaction's refId |
| 18 | + $refId = 'ref' . time(); |
| 19 | + |
| 20 | + // Create a Customer Profile Request |
| 21 | + // 1. (Optionally) create a Payment Profile |
| 22 | + // 2. (Optionally) create a Shipping Profile |
| 23 | + // 3. Create a Customer Profile (or specify an existing profile) |
| 24 | + // 4. Submit a CreateCustomerProfile Request |
| 25 | + // 5. Validate Profile ID returned |
| 26 | + |
| 27 | + // Set the payment data for the payment profile to a token obtained from Accept.js |
| 28 | + $op = new AnetAPI\OpaqueDataType(); |
| 29 | + $op->setDataDescriptor("COMMON.ACCEPT.INAPP.PAYMENT"); |
| 30 | + $op->setDataValue("119eyJjb2RlIjoiNTBfMl8wNjAwMDUyN0JEODE4RjQxOUEyRjhGQkIxMkY0MzdGQjAxQUIwRTY2NjhFNEFCN0VENzE4NTUwMjlGRUU0M0JFMENERUIwQzM2M0ExOUEwMDAzNzlGRDNFMjBCODJEMDFCQjkyNEJDIiwidG9rZW4iOiI5NDkwMjMyMTAyOTQwOTk5NDA0NjAzIiwidiI6IjEuMSJ9"); |
| 31 | + $paymentOne = new AnetAPI\PaymentType(); |
| 32 | + $paymentOne->setOpaqueData($op); |
| 33 | + |
| 34 | + |
| 35 | + // Create the Bill To info for new payment type |
| 36 | + $billto = new AnetAPI\CustomerAddressType(); |
| 37 | + $billto->setFirstName("Ellen"); |
| 38 | + $billto->setLastName("Johnson"); |
| 39 | + $billto->setCompany("Souveniropolis"); |
| 40 | + $billto->setAddress("14 Main Street"); |
| 41 | + $billto->setCity("Pecan Springs"); |
| 42 | + $billto->setState("TX"); |
| 43 | + $billto->setZip("44628"); |
| 44 | + $billto->setCountry("USA"); |
| 45 | + $billto->setPhoneNumber($phoneNumber); |
| 46 | + $billto->setfaxNumber("999-999-9999"); |
| 47 | + |
| 48 | + // Create a new Customer Payment Profile object |
| 49 | + $paymentprofile = new AnetAPI\CustomerPaymentProfileType(); |
| 50 | + $paymentprofile->setCustomerType('individual'); |
| 51 | + $paymentprofile->setBillTo($billto); |
| 52 | + $paymentprofile->setPayment($paymentOne); |
| 53 | + $paymentprofile->setDefaultPaymentProfile(true); |
| 54 | + |
| 55 | + $paymentprofiles[] = $paymentprofile; |
15 | 56 |
|
16 | | - // Create the payment data for an accept token |
17 | | - $op = new AnetAPI\OpaqueDataType(); |
18 | | - $op->setDataDescriptor("COMMON.ACCEPT.INAPP.PAYMENT"); |
19 | | - $op->setDataValue("9475089993864215505001"); |
20 | | - $paymentOne = new AnetAPI\PaymentType(); |
21 | | - $paymentOne->setOpaqueData($op); |
| 57 | + // Create a new CustomerProfileType and add the payment profile object |
| 58 | + $customerprofile = new AnetAPI\CustomerProfileType(); |
| 59 | + $customerprofile->setDescription("Customer Test PHP Accept Test"); |
22 | 60 |
|
23 | | - // Create the Bill To info |
24 | | - $billto = new AnetAPI\CustomerAddressType(); |
25 | | - $billto->setFirstName("Ellen"); |
26 | | - $billto->setLastName("Johnson"); |
27 | | - $billto->setCompany("Souveniropolis"); |
28 | | - $billto->setAddress("14 Main Street"); |
29 | | - $billto->setCity("Pecan Springs"); |
30 | | - $billto->setState("TX"); |
31 | | - $billto->setZip("44628"); |
32 | | - $billto->setCountry("USA"); |
33 | | - |
34 | | - // Create a Customer Profile Request |
35 | | - // 1. create a Payment Profile |
36 | | - // 2. create a Customer Profile |
37 | | - // 3. Submit a CreateCustomerProfile Request |
38 | | - // 4. Validate Profiiel ID returned |
| 61 | + $customerprofile->setMerchantCustomerId("M_".$email); |
| 62 | + $customerprofile->setEmail($email); |
| 63 | + $customerprofile->setPaymentProfiles($paymentprofiles); |
39 | 64 |
|
40 | | - $paymentprofile = new AnetAPI\CustomerPaymentProfileType(); |
| 65 | + // Assemble the complete transaction request |
| 66 | + $request = new AnetAPI\CreateCustomerProfileRequest(); |
| 67 | + $request->setMerchantAuthentication($merchantAuthentication); |
| 68 | + $request->setRefId($refId); |
| 69 | + $request->setProfile($customerprofile); |
41 | 70 |
|
42 | | - $paymentprofile->setCustomerType('individual'); |
43 | | - $paymentprofile->setBillTo($billto); |
44 | | - $paymentprofile->setPayment($paymentOne); |
45 | | - $paymentprofiles[] = $paymentprofile; |
46 | | - $customerprofile = new AnetAPI\CustomerProfileType(); |
47 | | - $customerprofile->setDescription("Customer Test PHP Accept Test"); |
| 71 | + // Create the controller and get the response |
| 72 | + $controller = new AnetController\CreateCustomerProfileController($request); |
| 73 | + $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); |
48 | 74 |
|
49 | | - $customerprofile->setMerchantCustomerId("M_".$email); |
50 | | - $customerprofile->setEmail($email); |
51 | | - $customerprofile->setPaymentProfiles($paymentprofiles); |
| 75 | + if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) { |
| 76 | + echo "Succesfully created customer profile : " . $response->getCustomerProfileId() . "\n"; |
| 77 | + $paymentProfiles = $response->getCustomerPaymentProfileIdList(); |
| 78 | + echo "SUCCESS: PAYMENT PROFILE ID : " . $paymentProfiles[0] . "\n"; |
| 79 | + } else { |
| 80 | + echo "ERROR : Invalid response\n"; |
| 81 | + $errorMessages = $response->getMessages()->getMessage(); |
| 82 | + echo "Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n"; |
| 83 | + } |
| 84 | + return $response; |
| 85 | +} |
52 | 86 |
|
53 | | - $request = new AnetAPI\CreateCustomerProfileRequest(); |
54 | | - $request->setMerchantAuthentication($merchantAuthentication); |
55 | | - $request->setRefId( $refId); |
56 | | - $request->setProfile($customerprofile); |
57 | | - $controller = new AnetController\CreateCustomerProfileController($request); |
58 | | - $response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX); |
59 | | - if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) |
60 | | - { |
61 | | - echo "Succesfully create customer profile : " . $response->getCustomerProfileId() . "\n"; |
62 | | - $paymentProfiles = $response->getCustomerPaymentProfileIdList(); |
63 | | - echo "SUCCESS: PAYMENT PROFILE ID : " . $paymentProfiles[0] . "\n"; |
64 | | - } |
65 | | - else |
66 | | - { |
67 | | - echo "ERROR : Invalid response\n"; |
68 | | - $errorMessages = $response->getMessages()->getMessage(); |
69 | | - echo "Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n"; |
70 | | - } |
71 | | - return $response; |
72 | | - } |
73 | | - if(!defined('DONT_RUN_SAMPLES')) |
74 | | - createCustomerProfileWithAcceptNonce("test123@test.com"); |
| 87 | +if (!defined('DONT_RUN_SAMPLES')) { |
| 88 | + createCustomerProfileWithAcceptNonce("test123@test.com"); |
| 89 | +} |
75 | 90 | ?> |
0 commit comments