Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditMemoCreateRefundTest.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
17  const RESOURCE_PATH = '/V1/creditmemo/refund';
18 
19  const SERVICE_READ_NAME = 'salesCreditmemoManagementV1';
20 
21  const SERVICE_VERSION = 'V1';
22 
26  protected $objectManager;
27 
28  protected function setUp()
29  {
31  }
32 
36  public function testInvoke()
37  {
39  $orderCollection = $this->objectManager->get(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
40  $order = $orderCollection->getFirstItem();
41  $items = [];
42 
43  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
44  $items = $this->getItemsForRest($order);
45  } else {
46  $items = $this->getItemsForSoap($order);
47  }
48 
49  $serviceInfo = [
50  'rest' => [
51  'resourcePath' => self::RESOURCE_PATH,
53  ],
54  'soap' => [
55  'service' => self::SERVICE_READ_NAME,
56  'serviceVersion' => self::SERVICE_VERSION,
57  'operation' => self::SERVICE_READ_NAME.'refund',
58  ],
59  ];
60  $data = [
61  'order_id' => $order->getId(),
62  'subtotal' => $order->getSubtotal(),
63  'grand_total' => $order->getGrandTotal(),
64  'base_grand_total' => $order->getBaseGrandTotal(),
65  'base_shipping_amount' => $order->getBaseShippingAmount(),
66  'shipping_address_id' => $order->getShippigAddressId(),
67  'billing_address_id' => $order->getBillingAddressId(),
68  'invoiceId' => $order->getInvoiceCollection()->getFirstItem()->getId(),
69  'adjustment' => null,
70  'adjustment_negative' => null,
71  'adjustment_positive' => null,
72  'base_adjustment' => null,
73  'base_adjustment_negative' => null,
74  'base_adjustment_positive' => null,
75  'base_currency_code' => null,
76  'base_discount_amount' => null,
77  'base_discount_tax_compensation_amount' => null,
78  'base_shipping_discount_tax_compensation_amnt' => null,
79  'base_shipping_incl_tax' => null,
80  'base_shipping_tax_amount' => null,
81  'base_subtotal' => null,
82  'base_subtotal_incl_tax' => null,
83  'base_tax_amount' => null,
84  'base_to_global_rate' => null,
85  'base_to_order_rate' => null,
86  'created_at' => null,
87  'creditmemo_status' => null,
88  'discount_amount' => null,
89  'discount_description' => null,
90  'email_sent' => null,
91  'entity_id' => null,
92  'global_currency_code' => null,
93  'discount_tax_compensation_amount' => null,
94  'increment_id' => null,
95  'invoice_id' => null,
96  'order_currency_code' => null,
97  'shipping_amount' => null,
98  'shipping_discount_tax_compensation_amount' => null,
99  'shipping_incl_tax' => null,
100  'shipping_tax_amount' => null,
101  'state' => null,
102  'store_currency_code' => null,
103  'store_id' => null,
104  'store_to_base_rate' => null,
105  'store_to_order_rate' => null,
106  'subtotal_incl_tax' => null,
107  'tax_amount' => null,
108  'transaction_id' => null,
109  'updated_at' => null,
110  'items' => $items,
111  ];
112  $result = $this->_webApiCall(
113  $serviceInfo,
114  ['creditmemo' => $data, 'offline_requested' => true]
115  );
116  $this->assertNotEmpty($result);
117  $order = $this->objectManager->get(OrderRepositoryInterface::class)->get($order->getId());
118  $this->assertEquals(Order::STATE_CLOSED, $order->getState());
119  }
120 
121  private function getItemsForRest($order)
122  {
123  $items = [];
125  foreach ($order->getAllItems() as $orderItem) {
126  $items[] = [
127  'order_item_id' => $orderItem->getId(),
128  'qty' => $orderItem->getQtyInvoiced(),
129  'price' => $orderItem->getPrice(),
130  'row_total' => $orderItem->getRowTotal(),
131  'entity_id' => null,
132  ];
133  }
134  return $items;
135  }
136 
137  private function getItemsForSoap($order)
138  {
139  $items = [];
141  foreach ($order->getAllItems() as $orderItem) {
142  $items[] = array_merge($orderItem->getData(), [
143  'order_item_id' => $orderItem->getId(),
144  'qty' => $orderItem->getQtyInvoiced(),
145  'price' => $orderItem->getPrice(),
146  'row_total' => $orderItem->getRowTotal(),
147  'entity_id' => null,
148  ]);
149  }
150  return $items;
151  }
152 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$orderItem
Definition: order.php:30
$order
Definition: order.php:55
$items