Example #1
0
    public static function createFromJson($json)
    {
        $data = $json->data;

        $response = new Ulink_PaymentResponse();
        $response->setAmount(new Ulink_Money($data->amount));
        $response->setCurrency($data->currency);
        if (isset($json->id) && $json->id) {
            $response->setClientTransactionId($json->id);
        }
        if (isset($json->{'response-url'}) && $json->{'response-url'}) {
            $request->setResponseUrl($json->{'response-url'});
        }
        if (isset($json->{'back-url'}) && $json->{'back-url'}) {
            $request->setGoBackUrl($json->{'back-url'});
        }
        if (isset($data->order)) {
            $response->setOrder(Ulink_Order::createFromJson($data->order));
        }
        $response->setSuccess($json->success);
        if (isset($data->test)) {
            $response->setTest($json->test);
        }
        $response->setErrors($json->errors);
        $response->setErrorCodes($json->errorCodes);

        return $response;
    }
Example #2
0
    public function testCreateRequestWithOrderFromJson()
    {
        $json = "{\"type\":\"pay-response\",\"timestamp\":123,\"id\":456,\"data\":{\"amount\":\"23.50\",\"currency\":\"EUR\"},\"success\":true,\"test\":true,\"errors\":[\"Wrong signature\"],\"errorCodes\":[17987]}";

        $response = Ulink_RequestFactory::createFromJson($json);
        $this->assertEquals(Ulink_PaymentResponse::clazz(), get_class($response));
        $this->assertEquals(123, $response->getTimestamp());

        $paymentResponse = $response;
        $this->assertEquals(new Ulink_Money("23.50"), $paymentResponse->getAmount());
        $this->assertEquals("EUR", $paymentResponse->getCurrency());
        $this->assertEquals(123, $paymentResponse->getTimestamp());
        $this->assertTrue($paymentResponse->isSuccess());
        $this->assertTrue($paymentResponse->isTest());
        $this->assertEquals(1, count($paymentResponse->getErrors()));
        $errors = $paymentResponse->getErrors();
        $this->assertEquals("Wrong signature", $errors[0]);
        $this->assertEquals(1, count($paymentResponse->getErrorCodes()));
        $codes = $paymentResponse->getErrorCodes();
        $this->assertEquals(17987, $codes[0]);
    }
 /**
  * @throws Exception\UlinkException
  * @param string $rawData
  * @return array
  */
 public function decrypt($rawData)
 {
     $packet = Ulink_TransportPacket::createFromJson($rawData);
     if (!$packet) {
         throw new UlinkException('Can not decrypt packet!');
     }
     if ($this->getClientId() != $packet->getClientId()) {
         throw new UlinkException('Client id does not match the id given in configuration!');
     }
     if (!$packet->getSignature()) {
         throw new UlinkException('Packet signature is broken!');
     }
     if (!$packet->validateAgainstKey($this->getPublicKeyPem())) {
         throw new UlinkException('Data signature does not match the packet content!');
     }
     $responseJson = Ulink_CryptoUtils::unseal($packet->getRequest(), $this->getPrivateKeyPem());
     $response = Ulink_RequestFactory::createFromJson($responseJson);
     $result = array('clientTransactionId' => $response->getClientTransactionId(), 'amount' => (string) $response->getAmount(), 'currency' => $response->getCurrency());
     $goBackUrl = $response->getGoBackUrl();
     if ($goBackUrl) {
         $result['goBackUrl'] = $goBackUrl;
     }
     $responseUrl = $response->getResponseUrl();
     if ($responseUrl) {
         $result['responseUrl'] = $responseUrl;
     }
     if (Ulink_PaymentResponse::clazz() == get_class($response)) {
         $result = array_merge($result, array('timestamp' => $response->getTimestamp(), 'success' => $response->isSuccess(), 'errors' => $response->getErrors(), 'errorCodes' => $response->getErrorCodes(), 'isTest' => $response->isTest()));
     }
     $order = $response->getOrder();
     if ($order && count($order->getItems())) {
         $items = $order->getItems();
         $result['order'] = array();
         foreach ($items as $item) {
             $result['order'][] = array('name' => $item->getName(), 'description' => $item->getDescription(), 'oneItemPrice' => (string) $item->getOneItemPrice(), 'quantity' => $item->getQuantity());
         }
     }
     return $result;
 }