Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*******************************************************************************
3
 *  Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *
6
 *  You may not use this file except in compliance with the License.
7
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
8
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
 *  specific language governing permissions and limitations under the License.
11
 * *****************************************************************************
12
 */
13
require_once ('CheckoutByAmazon/Service/Interface.php');
14
 
15
class CheckoutByAmazon_Service_CBAPurchaseContract implements CheckoutByAmazon_Service_Interface
16
{
17
var $service;
18
 
19
    public function __construct()
20
    {
21
        $this->getServiceObject();
22
    }
23
 
24
    /**
25
     * This function calls createPurchaseContract API and returns the PurchaseContract ID.
26
     * If you use this API, you must pass the Purchase Contract ID as input to the InlineCheckoutWidget.
27
     * If no Purchase Contract ID is passed to the InlineCheckoutWidget, the widget will always create
28
     * and return a new Purchase Contract ID.For most cases, you don't need to use this API. A
29
     * Purchase Contract ID will be returned to you from the InlineCheckoutWidget, which you can then
30
     * use with the other APIs.
31
     * @param void
32
     * @return String PurchaseContractId
33
     * @throws CheckoutByAmazon_Service_RequestException ,CheckoutByAmazon_Service_Exception
34
     *
35
     */
36
    public function createPurchaseContract()
37
    {
38
        $request = new CheckoutByAmazon_Service_Model_CreatePurchaseContractRequest();
39
 
40
          try
41
          {
42
             $response = $this->service->createPurchaseContract($request);
43
             if ($response->isSetCreatePurchaseContractResult())
44
             {
45
                    $createPurchaseContractResult = $response->getCreatePurchaseContractResult();
46
                    if ($createPurchaseContractResult->isSetPurchaseContractId())
47
                    {
48
                            return $createPurchaseContractResult->getPurchaseContractId();
49
                    }
50
             }
51
          }
52
          catch (CheckoutByAmazon_Service_Exception $ex)
53
          {
54
              $this->createCbaServiceException($ex);
55
 
56
          }
57
    }
58
 
59
    //Function to create service Object
60
    //Sets to Production/SandBox Urls
61
    private function getServiceObject()
62
    {
63
            $this->service = new CheckoutByAmazon_Service_Client(CheckoutByAmazon_Service_MerchantValues::getInstance()->getAccessKey(),
64
                                                CheckoutByAmazon_Service_MerchantValues::getInstance()->getSecretKey());
65
 
66
    }
67
 
68
    /**
69
     *This function calls getPurchaseContract API and returns the Address List.
70
     *It returns the Destination information if the buyer selected an address. This can be used if we need to
71
     *calculate Promotions and Shipping charges based on the address selected by the user
72
     *@param String PurchaseContractId
73
     *
74
     *@return List<ShippingAddress>  of all the shipping addresses selected
75
     * @throws CheckoutByAmazon_Service_RequestException,CheckoutByAmazon_Service_Exception
76
     */
77
    public function getAddress($purchaseContractId)
78
    {
79
             $request = new CheckoutByAmazon_Service_Model_GetPurchaseContractRequest(
80
                           array('PurchaseContractId' => $purchaseContractId));
81
             $addressArray = array();
82
             $addressCount = 0;
83
             try
84
             {
85
                  $response = $this->service->getPurchaseContract($request);
86
                  if ($response->isSetGetPurchaseContractResult())
87
                  {
88
                     $getPurchaseContractResult = $response->getGetPurchaseContractResult();
89
                    if ($getPurchaseContractResult->isSetPurchaseContract())
90
                    {
91
                       $purchaseContract = $getPurchaseContractResult->getPurchaseContract();
92
                       if($purchaseContract->isSetDestinations())
93
                        {
94
                            $destinations = $purchaseContract->getDestinations();
95
                            $destinationList = $destinations->getDestination();
96
                           foreach ($destinationList as $destination)
97
                            {
98
 
99
                            $state = $purchaseContract->getState();
100
                            $physicalDestinationAttributes = $destination->getPhysicalDestinationAttributes();
101
                                if ($physicalDestinationAttributes->isSetShippingAddress())
102
                                {
103
                                        $shippingAddressList[$addressCount++] = $physicalDestinationAttributes->getShippingAddress();
104
                                }
105
                            }
106
                        }
107
                    }
108
             }
109
             return $shippingAddressList;
110
             }
111
             catch (CheckoutByAmazon_Service_Exception $ex)
112
             {
113
                $this->createCbaServiceException($ex);
114
 
115
             }
116
    }
117
 
118
    /**
119
     * This function calls SetPurchaseItems API.The SetPurchaseItems API will take the list
120
     * of order items as input. You can specify the order total (that is, the
121
     * amount the buyer will be charged for the entire order) broken down to each item as part of this
122
     * API call. Please look at the sample code to see how each value can be set for the Item
123
     * @param String PurchaseContract ID , List<PurchaseItem> Items
124
     * @return int   1 if setting items was successful.
125
     * @throws CheckoutByAmazon_Service_RequestException,CheckoutByAmazon_Service_Exception
126
     */
127
    public function setItems($purchaseContractId,$itemList)
128
    {
129
         $request = new CheckoutByAmazon_Service_Model_SetPurchaseItemsRequest();
130
         $request->setPurchaseContractId($purchaseContractId);
131
         $request->setPurchaseItems($itemList);
132
         try
133
         {
134
             $response = $this->service->setPurchaseItems($request);
135
             return 1;
136
         }
137
         catch (CheckoutByAmazon_Service_Exception $ex)
138
         {
139
             $this->createCbaServiceException($ex);
140
 
141
         }
142
    }
143
 
144
    /**
145
     * This function is used to complete the order after setting the Items and contract charges or just the Items only.
146
     * This API transforms the purchase contract into Checkout by Amazon orders and
147
     * returns you a list of Checkout by Amazon Order IDs.
148
     * @param String PurchaseContractId
149
     * @return List of all Order ids
150
     * @throws CheckoutByAmazon_Service_RequestException ,CheckoutByAmazon_Service_Exception
151
     */
152
    public function completeOrder($purchaseContractId)
153
    {
154
        $request = new CheckoutByAmazon_Service_Model_CompletePurchaseContractRequest();
155
        try
156
        {
157
            $request->setPurchaseContractId($purchaseContractId);
158
            $response = $this->service->completePurchaseContract($request);
159
            if ($response->isSetCompletePurchaseContractResult())
160
            {
161
                $completePurchaseContractResult = $response->getCompletePurchaseContractResult();
162
                if ($completePurchaseContractResult->isSetOrderIds())
163
                {
164
                    $orderIds = $completePurchaseContractResult->getOrderIds();
165
                    $orderIdList  =  $orderIds->getOrderId();
166
                    return $orderIdList;
167
                }
168
            }
169
        }
170
        catch (CheckoutByAmazon_Service_Exception $ex)
171
        {
172
            $this->createCbaServiceException($ex);
173
 
174
        }
175
 
176
    }
177
 
178
   /**
179
     * This function calls SetContractCharges API. You can use this API to set shipping
180
     * or promotion amounts for the entire purchase contract.After the purchase contract is
181
     * completed (that is, the order is placed), the contract charges are distributed to each item
182
     * proportional to the item's cost (that is, the item's unit price multiplied by the quantity).
183
     * However, the sum of charge amounts distributed to each item will be equal to the contract charge
184
     * amount you set with this API.Please refer to the sample codes to see how this can be set.
185
     * There can be following valid sets of input charge elements to this API –
186
     * Shipping + Promotion
187
     * Shipping + No Promotion
188
     * No Shipping + Promotion
189
     * @param String PurchaseContractId, Charges ChargesObject
190
     * @return int 1 if setting contract charges was successful.
191
     * @throws CheckoutByAmazonServiceRequestException,CheckoutByAmazonServiceException
192
     */
193
    public function setContractCharges($purchaseContractId, $charges)
194
     {
195
         $request = new CheckoutByAmazon_Service_Model_SetContractChargesRequest();
196
         $request->setPurchaseContractId($purchaseContractId);
197
         $request->setCharges($charges);
198
         try
199
         {
200
             $response = $this->service->setContractCharges($request);
201
             return 1;
202
         }
203
         catch (CheckoutByAmazon_Service_Exception $ex)
204
         {
205
             $this->createCbaServiceException($ex);
206
         }
207
     }
208
 
209
    private function createCbaServiceException(CheckoutByAmazon_Service_Exception $ex)
210
    {
211
        if(($ex->getStatusCode() == 400) || ($ex->getStatusCode() == 403))
212
        {
213
            throw new CheckoutByAmazon_Service_RequestException($ex->getErrorCode(),$ex->getMessage(),$ex->getStatusCode(),
214
                                                                        $ex->getErrorType(),$ex->getRequestId(),$ex->getXML());
215
        }
216
        else
217
        {
218
            throw $ex;
219
        }
220
 
221
    }
222
 
223
}
224
?>