Alipay is the focus of our payment, in order to settle the Alipay payment we spent a lot of time and energy, but also went through numerous pits. And today in the generation of alipay small program two-dimensional code on a small interface, we were pit ~~~

First scene of accident



Afternoon suddenly received a colleague’s call for help, said to pay treasure private key has a problem, a check, found no problem ah. I looked at my colleagues’ questions as follows:

  1. Scenario 1: Debug the development environment (Windows), send an interface request, and return to normal
  2. Scenario 2, publish to server (Docker) environment, send interface request, return failure. The error exception is as follows:
rid:3cbfe2ae41f44b64b01f31b26a2197de . url:/xxx/xxxAppQrCode
   Execute Time:54 ms
   code:500,msg:ServerError:	[Exception]:[serviceName]: [computerName]:c7bb3eb3867a [workPath]:/publish/ [processInfo]:13-dotnet [computerAddress]: 172.17.0.7
   [ExceptionMessage]: The private key format you are using is incorrect. Please check the RSA private key configuration,charset = utf-8
   [ExceptionStackTrace]:   at Aop.Api.Util.AlipaySignature.RSASignCharSet(String data, String privateKeyPem, String charset, Boolean keyFromFile, String signType)
  at Aop.Api.Util.AlipaySignature.RSASign(IDictionary`2 parameters, String privateKeyPem, String charset, Boolean keyFromFile, String signType)
  at Aop.Api.Util.AopUtils.SignAopRequest(IDictionary`2 parameters, String privateKeyPem, String charset, Boolean keyFromFile, String signType)
  at Aop.Api.DefaultAopClient.Execute[T](IAopRequest`1 request, String accessToken, String appAuthToken)
  at Aop.Api.DefaultAopClient.Execute[T](IAopRequest`1 request, String accessToken)
  at Aop.Api.DefaultAopClient.Execute[T](IAopRequest`1 request)
Copy the code

Ha ha, unexpectedly encountered such a magical mistake.

Troubleshooting 1: The configuration is incorrect

Check the configuration of the development environment, compare the configuration of the server environment, copy and copy it, and find that the configuration is the same.

This road is blocked!

Check 2: Baidu

In order to help my colleague solve this problem, I carried out a rescue work. Search Baidu, a few times to find a solution to the blog park.

Please check the RSA private key configuration,charset = UTF-8. The reason is that the private key in my code is directly copied the code in the PEM file, but alipay’s underlying SDK uses the file by default, method reference

Defaultaopclient. cs: =false (keyFromFile =false); The HttpRuntime. AppDomainAppPath. ToString () + “App_Data \ ant \ rsa_private_key pem”

This is a point, I went back and asked my colleague, said it had been set, open the pit is quite deep ah!

Investigation 3: ask experienced pay treasure development expert

Claims to have 3 years of experience in payment development, inherited the major payment platform, is the pillar of our company! Key time, need veteran horse! I got a call and he said, “Try it for me.”

  • Alipay private key check, passed
  • Alipay public key verification, passed
  • Local debugging, pass
  • See log: Stuck in the private key format you are using, please check the RSA private key configuration.

55555, no one can help my dear colleague! I guess it’s time for me to pull out my magic ladder, flip through foreign literature, and not help! It seems that there is no shortcut, but also an honest look at the source ~~~

Check 4: Github source code

Source code address: github.com/alipay/alip… Check the source code, encoding using UTF-8, no problem, then pull down the source code, after all, there is no direct trace in the Docker container, it is better to load the source code, print out the request parameters, see what the situation. Say dry dry, pull down the source code, change the library.

Gee, gee, what is this?



Our dear colleague, introduced

<PackageReference Include="Alipay" Version="2.0.0" />
Copy the code

This library does not support.net Core!

oh,my god!

B: I see. It’s the library. It must be.

Look at the official instructions!!



I want to die heart already have!

Replace the correct bag

What else is there to say? Linux requires special libraries, so libraries for.net Core should come first. As for the magic question, it can only be said that the.net Framework under the library on Windows can also run on.net Core, is really magic!

Good, I big pay treasure, do not carry your rotten pot!

Wish the world no magic hand, then the world no magic pit!