preface

Since the company’s APP needs to be put into use in Sweden, it needs to access local Swish payment. However, there are only a few simple documents about this payment method on their official website, no Demo on Github, and almost no information on the Internet. Therefore, here is a summary of the process of accessing this payment. Hopefully it will be helpful for students who need to use Swish payment. Here is the address of the developer documentation official Swish: developer. Getswish. Se/merchants

Why access Swish payments?

Swish is a mobile payment APP used by more than half of Sweden’s population of 9.7 million. Swish is used by more than 5 million people and supports six Swedish banks. So Swish’s status in Sweden is the same as wechat Pay and Alipay are in China.

1. Configure the background certificate environment for Swish

This step is done by background of students, need to enter this web page configuration Swish certificate, comcert.getswish.net/cert-mgmt-w… The CSR file required during the configuration is generated on the server and then copied into the input box, as shown in the following figure

Then a PEM certificate file will be generated, as shown in the following figure. Copy the contents of the PEM certificate file and save the contents in a new PEM certificate file

2. The APP obtains the token

The APP passes the payment amount, the currency (Swish currently only supports SEK, so all currencies except Kr are not supported), the payment describes these values to the background interface, and the background returns the token(which is used to jump to the Swish APP for payment) paymentID(which is later used to verify the payment status). Below is my request this interface code example, for reference only

/ / send a payment request (swish) - (RACSignal *) getSwishPaymentTokenSignal: (nsstrings *) amount currency: currency nsstrings *) desc:(NSString *)desc{ @weakify(self)return [[[self.services.client getSwishPaymentToken:amount
                                               currency:currency
                                                   desc:desc] doNext:^(id  _Nullable response) {
        @strongify(self)
        self.clientToken = response[@"token"];
        self.paymentId = response[@"id"];
    }] takeUntil:self.rac_willDeallocSignal];
}
Copy the code

3. The background generates the token and returns it to the APP

The background calls the request payment interface (here is the interface of the formal environment, because the Swish test environment cannot conduct the test of using the Swish APP payment, so I directly use the formal environment here) cpc.getswish.net/swish-cpcap… Here is the sample code for the background request, where the certificate file is in P12 format and is generated directly from the PEM certificate from the previous step:

curl -v --data '{ "payeePaymentReference": "0123456789", "callbackUrl":
"https://example.com/api/swishcb/paymentrequests", "payeeAlias":
"1231181189", "amount": "100", "currency": "SEK", "message": "Kingston
USB Flash Drive 8 GB" }' -H "Content-Type: application/json" POST
https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests --cert
"Swish Merchant Test Certificate 1231181189.p12:swish" --cert-type p12 --
cacert "Swish TLS Root CA.pem"
Copy the code

This interface returns Token and paymentID, which are returned in the request header, as shown below

< HTTP/1.1 201 < Location: https://mss.cpc.getswish.net/swishcpcapi/api/v1/paymentrequests/11A86BE70EA346E4B1C39C874173F088 < Server: Nginx / 1.12.1 < Connection: keep - the alive < PaymentRequestToken: ed16db6f415145ec93642e294c904378 < Content - Length: 0 < Date: Fri, 04 Jan 2019 08:34:59 GMT <Copy the code

4. APP invokes Swish APP for payment

Before calling Swish APP for payment, the Scheme of current application should be configured as shown in the figure below. In URL Types of project INFO, URL Schemes of URL Schemes should be configured. Here I write my application name splicing swish.com

Use the token to jump to Swish APP for payment, and the code is as follows: XXX1 below is URL Schemes configured in the figure above, xxx2 is the Identifier in the figure above. It must be correctly configured, otherwise it will not jump back to your APP.

- (void)swishPay:(NSString *)token{
    if(@available(iOS 10.0, *)) {[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"swish://paymentrequest? token=%@&callbackurl=xxx1://xxx2.swish.com",token]] options:@{} completionHandler:^(BOOL success) {
            
        }];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"swish://paymentrequest? token=%@&callbackurl=xxx1://xxx2.swish.com",token]]]; }}Copy the code

5. Verify that the Swish payment is successful

After making a payment in the Swish APP, it will return to its own APP and write the following code in Appdelegate. m to listen for Swish to jump back to the current APP. Since Swish Payment does not provide a payment callback interface, a manual notification is sent to the payment page, requesting the background to determine whether the payment is successful.

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    if([url.absoluteString containsString:@"swish"]) {/ / swish pay jump back [MHNotificationCenter postNotificationName: SDSwishPayResultNotification object: nil]; }return YES;
}
Copy the code

After listening for notifications that need to be sent, pass paymentId(which the background has already returned) to the background and invoke the Swish validation interface of the background to verify that the payment was successful.

6. Background verification of Swish payment

/ API /v1/ paymentRequests /{id} Sample request code is shown below:

curl -v "Content-Type: application/json" GET
https://mss.cpc.getswish.net/swishcpcapi/api/v1/paymentrequests/5D59DA1B1632424E874DDB219AD54597 --cert
"Swish Merchant Test Certificate 1231181189.p12:swish" --cert-type p12 --
cacert "Swish TLS Root CA.pem"
Copy the code

Sample code is returned as follows:

< HTTP/1.1 200 < content-type: application/json; charset=UTF-8 < Transfer-Encoding: chunked < Date: Fri, 04 Jan 2019 09:00:29 GMT < * Connection#1 to host mss.cpc.getswish.net left intact
{"id":"5D59DA1B1632424E874DDB219AD54597"."payeePaymentReference":"0123456, 789"."paymentReference":"1E2FC19E5E5E4E18916609B7F8911C12"."callbackUrl":
"https://example.com/api/swishcb/paymentrequests"."payerAlias":"467123476"."payeeAlias":"1231181189"."amount": 100.00."currency":"SEK"."message":"
Kingston USB Flash Drive 8 GB"."status":"PAID"."dateCreated":"2019-01-02 t14:29:51. 092 z"."datePaid":"2019-01-02 t14:29:55. 093 z"."errorCode":null,"errorMessage":""}
Copy the code

If it can be found that the status of the payment is PAID, it means that Swish payment is successful. You can return success to APP in the interface.

conclusion

Above is Swish pay for access to the whole process analysis, if you need a refund function, can consult the API documentation, and pay the interface of the content is almost, this is the address of the official document Swsih pay: developer. Getswish. Se/content/upl…

Looking forward to

  • If the article is of some help, please give a like or comment; If not helpful, please give some advice ~

  • If you have any questions, please point them out in the comments at the bottom of this article, and I will quickly solve and correct the problem.