示例#1
0
    /**
     * Test to create a Digest hash of a json encoded hash.
     *
     * @return void
     */
    public function testCreateDigest()
    {
        $expected = 'MO/6KvzsY2y+F+/SexH7Hyg16gFpsPDx5A2PtLZd0Zs=';

        $digester = new Klarna_Checkout_Digest;

        $json = array(
            'eid' => 1245,
            'goods_list' => array(
                array(
                    'artno' => 'id_1',
                    'name' => 'product',
                    'price' => 12345,
                    'vat' => 25,
                    'qty' => 1
                )
            ),
            'currency' => 'SEK',
            'country' => 'SWE',
            'language' => 'SV'
        );

        $this->assertEquals(
            $expected,
            $digester->create(json_encode($json).'mySecret'),
            'JsonEncoded Hash Digest.'
        );
    }
 /**
  * Set content (headers, payload) on a request
  *
  * @param Klarna_Checkout_ResourceInterface $resource Klarna Checkout Resource
  * @param string                            $method   HTTP Method
  * @param string                            $payload  Payload to send with the
  *                                                    request
  * @param string                            $url      URL for request
  *
  * @return Klarna_Checkout_HTTP_Request
  */
 protected function createRequest(Klarna_Checkout_ResourceInterface $resource, $method, $payload, $url)
 {
     // Generate the digest string
     $digest = $this->digester->create($payload . $this->_secret);
     $request = $this->http->createRequest($url);
     $request->setMethod($method);
     // Set HTTP Headers
     $request->setHeader('User-Agent', (string) $this->userAgent());
     $request->setHeader('Authorization', "Klarna {$digest}");
     $request->setHeader('Accept', $resource->getContentType());
     if (strlen($payload) > 0) {
         $request->setHeader('Content-Type', $resource->getContentType());
         $request->setData($payload);
     }
     return $request;
 }