2013-10-14 4 views
2

cakephp에 새로운 것이고 Webshop Solution Snipcart를 구현했습니다. 주문이 스 니펫 카트에 의해 처리되면 우리 사이트에 웹 훅을 보냅니다. 다음과 같습니다cakephp와 Webhooks

{ 
    eventName: "order:completed", 
     mode: "Live", 
     createdOn: "2013-07-04T04:18:44.5538768Z", 
     content: { 
      token: "22808196-0eff-4a6e-b136-3e4d628b3cf5", 
      creationDate: "2013-07-03T19:08:28.993Z", 
      modificationDate: "2013-07-04T04:18:42.73Z", 
      status: "Processed", 
      paymentMethod: "CreditCard", 
      email: "[email protected]", 
      cardHolderName: "Nicolas Cage", 
      billingAddressName: "Nicolas Cage", 
      billingAddressCompanyName: "Company name", 
      billingAddressAddress1: "888 The street", 
      billingAddressAddress2: "", 
      billingAddressCity: "Québec", 
      billingAddressCountry: "CA", 
      billingAddressProvince: "QC", 
      billingAddressPostalCode: "G1G 1G1", 
      billingAddressPhone: "(888) 888-8888", 
      shippingAddressName: "Nicolas Cage", 
      shippingAddressCompanyName: "Company name", 
      shippingAddressAddress1: "888 The street", 
      shippingAddressAddress2: "", 
      shippingAddressCity: "Québec", 
      shippingAddressCountry: "CA", 
      shippingAddressProvince: "QC", 
      shippingAddressPostalCode: "G1G 1G1", 
      shippingAddressPhone: "(888) 888-8888", 
      shippingAddressSameAsBilling: true, 
      finalGrandTotal: 310.00, 
      shippingAddressComplete: true, 
      creditCardLast4Digits: "4242", 
      shippingFees: 10.00, 
      shippingMethod: "Livraison", 
      items: [{ 
       uniqueId: "eb4c9dae-e725-4dad-b7ae-a5e48097c831", 
       token: "22808196-0eff-4a6e-b136-3e4d628b3cf5", 
       id: "1", 
       name: "Movie", 
       price: 300.00, 
       originalPrice: 300.00, 
       quantity: 1, 
       url: "https://snipcart.com", 
       weight: 10.00, 
       description: "Something", 
       image: "http://placecage.com/50/50", 
       customFieldsJson: "[]", 
       stackable: true, 
       maxQuantity: null, 
       totalPrice: 300.0000, 
       totalWeight: 10.00 
      }], 
      subtotal: 610.0000, 
      totalWeight: 20.00, 
      hasPromocode: false, 
      promocodes: [], 
      willBePaidLater: false 
     } 
} 

을 그리고는 webhooks 소비 : 다음은 webhook는 설명서에 따라 다음과 같습니다

<?php 

$json = file_get_contents('php://input'); 
$body = json_decode($json, true); 

if (is_null($body) or !isset($body['eventName'])) { 
    // When something goes wrong, return an invalid status code 
    // such as 400 BadRequest. 
    header('HTTP/1.1 400 Bad Request'); 
    return; 
} 

switch ($body['eventName']) { 
    case 'order:completed': 
     // This is an order:completed event 
     // do what needs to be done here. 
     break; 
} 

// Return a valid status code such as 200 OK. 
header('HTTP/1.1 200 OK'); 

내 질문은 내가 CakePHP의 버전 2.4에서이 작업을 수행 할 방법이다. 나는 해결책을 찾기 위해 인터넷에서 일을 찾고 있었지만, 나는 경험이 없기 때문에 적절한 해결책을 찾을 수 없다.

그것을 해결 : 나는 당신의 대답에 추가 할 수

public function webhooks(){ 

    //check if POST 
    if ($this->request->is('post')) { 
    //Allow raw POST's 
    $url = 'php://input'; 
    //decode 
    $json = json_decode(file_get_contents($url), true); 

    if (is_null($json) or !isset($json['eventName'])) { 
    // When something goes wrong, return an invalid status code 
    // such as 400 BadRequest. 
    header('HTTP/1.1 400 Bad Request'); 
    return; 
} 
    //do whatever needs to be done, in this case remove the quantity ordered from the stock in db. 
    switch ($json['eventName']) { 
    case 'order:completed': 

     $id = $json['content']['items'][0]['id']; 
     $quantity = $json['content']['items'][0]['quantity']; 

     $query = $this->Shop->findById($id, 'Shop.stock'); 

     $stock = $query['Shop']['stock']; 

     $stock = $stock - $quantity; 

    $this->Shop->updateAll(array('Shop.stock' => $stock), array('Shop.id' => $id)); 

    break; 

     } 

     header('HTTP/1.1 200 OK'); 

    } 
} 
+0

@CharlesOuellet, 어떻게 스택 오버플로가 작동하지 않습니다. 커뮤니티에 참여하려면 전자 메일이 아니라 여기에 답변을 게시하십시오. – Charles

답변

4

유일한 것은 어떤 일을 몇 가지 더 "케이크"방법이다.

public function webhooks(){ 

//check if POST 
if ($this->request->is('post') || $this->request->is('put')) { 
//Allow raw POST's 
$url = 'php://input'; 
//decode 
$json = json_decode(file_get_contents($url), true); 

if (empty($json) or empty($json['eventName'])) { 
    throw new BadRequestException('Invalid JSON Information'); 
} 
//do whatever needs to be done, in this case remove the quantity ordered from the stock in db. 
switch ($json['eventName']) { 
case 'order:completed': 

    $id = $json['content']['items'][0]['id']; 
    $quantity = $json['content']['items'][0]['quantity']; 

    $shop = $this->Shop->find('first', array(
     'conditions' => array(
       'Shop.id' => $id, 
     ), 
     'fields' => array(
       'Shop.stock', 
     ), 
    )); 
    if (empty($shop)) { 
     throw new NotFoundException(__('Invalid Shop Content')); 
    } 
    $stock = $shop['Shop']['stock']; 

    $stock = $stock - $quantity; 
$this->Shop->id = $id; 
$this->Shop->saveField('stock', $stock); 
break; 

    }