/** * @test */ public function readAndVerifyWithRealKeysKey() { // You can get a simple private/public key pair using: // openssl genrsa 512 >private_key.txt // openssl rsa -pubout <private_key.txt >public_key.txt $privKey = <<<EOD -----BEGIN RSA PRIVATE KEY----- MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z RZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQJAL151ZeMKHEU2c1qdRKS9 sTxCcc2pVwoAGVzRccNX16tfmCf8FjxuM3WmLdsPxYoHrwb1LFNxiNk1MXrxjH3R 6QIhAPB7edmcjH4bhMaJBztcbNE1VRCEi/bisAwiPPMq9/2nAiEA3lyc5+f6DEIJ h1y6BWkdVULDSM+jpi1XiV/DevxuijMCIQCAEPGqHsF+4v7Jj+3HAgh9PU6otj2n Y79nJtCYmvhoHwIgNDePaS4inApN7omp7WdXyhPZhBmulnGDYvEoGJN66d0CIHra I2SvDkQ5CmrzkW5qPaE2oO7BSqAhRZxiYpZFb5CI -----END RSA PRIVATE KEY----- EOD; $pubKey = <<<EOD -----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6 zxqlVzz0wy2j4kQVUC4ZRZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQ== -----END PUBLIC KEY----- EOD; $pair = new Ulink_KeyPair($privKey, $pubKey); $myData = "foobarbaz"; $this->assertNotNull($pair->getPrivateKey()); $signatrue = Ulink_CryptoUtils::sign($myData, $pair->getPrivateKey()); $this->assertNotNull($signatrue); $this->assertTrue(Ulink_CryptoUtils::isValidRSASignature("foobarbaz", $signatrue, $pair->getPublicKey())); $this->assertFalse(Ulink_CryptoUtils::isValidRSASignature("foo", $signatrue, $pair->getPublicKey())); $sealed = Ulink_CryptoUtils::seal($myData, $pair->getPublicKey()); $opened = Ulink_CryptoUtils::unseal($sealed, $pair->getPrivateKey()); $this->assertNotEquals($myData, $sealed); $this->assertEquals($myData, $opened); }
/** * @param array $data * @return string */ public function encrypt($data = array()) { $defaults = array('clientTransactionId' => '', 'amount' => '0', 'order' => array(), 'currency' => null, 'goBackUrl' => null, 'responseUrl' => null); $data = array_merge($defaults, $data); $request = new Ulink_PaymentRequest(); $request->setClientTransactionId($data['clientTransactionId']); $request->setAmount(new Ulink_Money($data['amount'])); $request->setCurrency($data['currency'] ? $data['currency'] : $this->getDefaultCurrency()); $request->setGoBackUrl($data['goBackUrl'] ? $data['goBackUrl'] : $this->getDefaultGoBackUrl()); $request->setResponseUrl($data['responseUrl'] ? $data['responseUrl'] : $this->getDefaultResponseUrl()); if (count($data['order'])) { $_order = new Ulink_Order(); /** * $item = array( * 'name' => 'Some Name', * 'description' => 'Some Description', * 'oneItemPrice' => '10.90', * 'quantity' => 5 * ); */ foreach ($data['order'] as $item) { $_order->addItem(new Ulink_OrderItem($item['name'], $item['description'], new Ulink_Money($item['oneItemPrice']), isset($item['quantity']) ? $item['quantity'] : 1)); } $request->setOrder($_order); } $requestJson = $request->toJson(); $requestJson = Ulink_CryptoUtils::seal($requestJson, $this->getPublicKeyPem()); $packet = new Ulink_TransportPacket(); $packet->setRequest($requestJson); $signature = Ulink_CryptoUtils::sign($requestJson, $this->getPrivateKeyPem()); $packet->setSignature($signature); $packet->setClientId($this->getClientId()); return $packet->toJson(); }
/** * @test * @return void */ public function testPaymentOut() { $privKey = $this->getPrivateKeyPem(); $pubKey = $this->getPublicKeyPem(); $order = new Ulink_Order(); $order->addItem(new Ulink_OrderItem("Milk", "Puhlqj ez", new Ulink_Money("25.90"))); $order->addItem(new Ulink_OrderItem("Mja4ik", "Puhlqj mja4", new Ulink_Money("9.00"))); $request = new Ulink_PaymentRequest(); $request->setAmount(new Ulink_Money("34.90")); $request->setCurrency(Ulink_Request::CURRENCY_EURO); $request->setOrder($order); $requestJson = $request->toJson(); $requestJson = Ulink_CryptoUtils::seal($requestJson, $pubKey); $packet = new Ulink_TransportPacket(); $packet->setRequest($requestJson); $signature = Ulink_CryptoUtils::sign($requestJson, $privKey); $packet->setSignature($signature); $packet->setClientId(15); $rawData = $packet->toJson(); $packet = Ulink_TransportPacket::createFromJson($rawData); $this->assertNotNull($packet); $this->assertEquals(15, $packet->getClientId()); $this->assertEquals($requestJson, $packet->getRequest()); $this->assertEquals($signature, $packet->getSignature()); $this->assertNotNull($packet->getSignature()); $this->assertTrue($packet->validateAgainstKey($pubKey)); $request = Ulink_RequestFactory::createFromJson( Ulink_CryptoUtils::unseal($packet->getRequest(), $privKey) ); $this->assertInstanceof('Ulink_PaymentRequest', $request); $paymentRequest = $request; $this->assertEquals("34.90", (string)$paymentRequest->getAmount()); $this->assertEquals("EUR", $paymentRequest->getCurrency()); $order = $paymentRequest->getOrder(); $this->assertNotNull($order); $this->assertEquals(2, count($order->getItems())); $items = $order->getItems(); $orderItem1 = $items[0]; $orderItem2 = $items[1]; $this->assertEquals("Milk", $orderItem1->getName()); $this->assertEquals("Puhlqj ez", $orderItem1->getDescription()); $this->assertEquals("25.90", (string)$orderItem1->getOneItemPrice()); $this->assertEquals("Mja4ik", $orderItem2->getName()); $this->assertEquals("Puhlqj mja4", $orderItem2->getDescription()); $this->assertEquals("9.00", (string)$orderItem2->getOneItemPrice()); }