-1

I need to develop Marketplace API to get orders and Post Shipping to Walmart in PHP. Their Code library/help sample is provided in Java. I am not good with Java.

Need help understanding / coding it in PHP. Here is the code in Java. Link to the API developers guide https://developer.walmartapis.com/#self-written-code-expert

This app is on Cpanel server.

import org.apache.commons.codec.binary.Base64;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;

public class SHA256WithRSAAlgo {
private static String consumerId = "b68d2a72....";   // Trimmed for security reason
private static String baseUrl = "https://marketplace.walmartapis.com/v2/feeds";
private static String privateEncodedStr = "MIICeAIBADANBgkqhkiG9w0BAQEFAA......";       //Trimmed for security reasons
public static void main(String[] args) {
    String httpMethod = "GET";
    String timestamp = String.valueOf(System.currentTimeMillis());
    String stringToSign = consumerId + "\n" +
                            baseUrl + "\n" +
                            httpMethod + "\n" +
                            timestamp + "\n";
    String signedString = SHA256WithRSAAlgo.signData(stringToSign, privateEncodedStr);
    System.out.println("Signed String: " + signedString);
}
public static String signData(String stringToBeSigned, String encodedPrivateKey) {
    String signatureString = null;
    try {
        byte[] encodedKeyBytes = Base64.decodeBase64(encodedPrivateKey);
        PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(encodedKeyBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey myPrivateKey = kf.generatePrivate(privSpec);
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(myPrivateKey);
        byte[] data = stringToBeSigned.getBytes("UTF-8");
        signature.update(data);
        byte[] signedBytes = signature.sign();
        signatureString = Base64.encodeBase64String(signedBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return signatureString;
}
}

Edit:

The Part which I did not understand is #3 on this page. which says Sign the byte array representation of this data by

https://developer.walmartapis.com/#self-written-code-expert

Good Lux
  • 896
  • 9
  • 19

1 Answers1

2

It's quite simple actually, Java imports packages for each source file as PHP include files. So the first lines are doing that and then the classes function pretty much as PHP5 classes. That's it. Specifying what exactly you don't understand will help better explaining the code...

Hadim
  • 33
  • 1
  • 6