helmutschneider / swish-php

PHP-wrapper for the Swish merchant api
MIT License
40 stars 19 forks source link

empty/null response #5

Closed elr0berto closed 7 years ago

elr0berto commented 7 years ago

My code:

<?php
require_once(__DIR__.'/vendor/swish-php/vendor/autoload.php');

use HelmutSchneider\Swish\Client;
use HelmutSchneider\Swish\Util;

// Swish CA root cert
$rootCert = __DIR__.'/../swish/cert_test/ca.crt'; // forwarded to guzzle's "verify" option

// .pem-bundle containing your client cert and it's corresponding private key. forwarded to guzzle's "cert" option
$clientCert = [__DIR__.'/../swish/cert_test/cl.pem', 'swish'];

$client = Client::make($rootCert, $clientCert, Client::SWISH_TEST_URL);

$response = $client->createPaymentRequest([
                                              'callbackUrl' => 'https://localhost/swish',
                                              'payeePaymentReference' => '12345',
                                              'payerAlias' => '4671234768',
                                              'payeeAlias' => '1231181189',
                                              'amount' => '100',
                                              'currency' => 'SEK',
                                          ]);

$data = Util::decodeResponse($response, true);
var_dump($response->getBody()); // empty stream 
var_dump($response); // show "Created" and statusCode 201 .. so seems successful?!
var_dump($data); // NULL

Response in command line:

/home/robert/work/tomtebrevet/tomtebrevet.se/tswish.php:26:
class GuzzleHttp\Psr7\Response#28 (6) {
  private $reasonPhrase =>
  string(7) "Created"
  private $statusCode =>
  int(201)
  private $headers =>
  array(4) {
    'Server' =>
    array(1) {
      [0] =>
      string(17) "Apache-Coyote/1.1"
    }
    'Location' =>
    array(1) {
      [0] =>
      string(100) "https://mss.swicpc.bankgirot.se/swish-cpcapi/api/v1/paymentrequests/D890F10E3AB047C39C3B2DB31605BC4F"
    }
    'Content-Length' =>
    array(1) {
      [0] =>
      string(1) "0"
    }
    'Date' =>
    array(1) {
      [0] =>
      string(29) "Wed, 26 Jul 2017 09:57:28 GMT"
    }
  }
  private $headerNames =>
  array(4) {
    'server' =>
    string(6) "Server"
    'location' =>
    string(8) "Location"
    'content-length' =>
    string(14) "Content-Length"
    'date' =>
    string(4) "Date"
  }
  private $protocol =>
  string(3) "1.1"
  private $stream =>
  class GuzzleHttp\Psr7\Stream#26 (7) {
    private $stream =>
    resource(44) of type (stream)
    private $size =>
    NULL
    private $seekable =>
    bool(true)
    private $readable =>
    bool(true)
    private $writable =>
    bool(true)
    private $uri =>
    string(10) "php://temp"
    private $customMetadata =>
    array(0) {
    }
  }
}
/home/robert/work/tomtebrevet/tomtebrevet.se/tswish.php:27:
NULL

Any idea why I don't get a full response?

helmutschneider commented 7 years ago

Please refer to the official swish docs. This library is not doing any manipulation of the responses so if you're getting 200 OK back you should be all good. Copy-pasta from the docs, section 11.1.3:

curl -v --request POST https://swicpc.bankgirot.se/swishcpcapi/api/v1/paymentrequests
\
 --header "Content-Type: application/json" \
 --data @- <<!
{
 "payeePaymentReference": "0123456789",
 "callbackUrl": "https://example.com/api/swishcb/paymentrequests",
 "payerAlias": "46701234567",
 "payeeAlias": "1234760039",
 "amount": "100",
 "currency": "SEK",
 "message": "Kingston USB Flash Drive 8 GB"
}
!
< HTTP/1.1 201 Created
< Location: https://swicpc.bankgirot.se/swishcpcapi/api/v1/paymentrequests/AB23D7406ECE4542A80152D909EF9F6B

As you can see the response only contains a header with a link to the payment request. You may use this method to extract the ID.

elr0berto commented 7 years ago

It works when i get the response from $client->getPaymentRequest() and then decode it .. not sure why... anyway, I'm happy that this works now :)

Thanks for the good work Helmut :)

$id = Util::getPaymentRequestIdFromResponse($res); $res = $client->getPaymentRequest($id); $body = Util::decodeResponse($res); // now it works ..